Behaviors in WPF introduction

I held a short introduction to behaviors in WPF this past weekend and I thought I would share it with a blog post.

First watch out for the spelling. In British the spelling is “behaviour” this is also a correct spelling but since Microsoft is from USA we should use the American spelling “behavior”. Also because you get more hits if you use the “behavior” spelling when you search online.

What is a behavior?

A behavior encapsulate pieces of functionality into a reusable component, which we later on can attach to a element in a view. Emphasis is on reusable. One can do the same code in codebehind or perhaps directly in XAML so it is nothing magic about a behavior. Behaviors also have the benefit of keeping the MVVM pattern intact, since we can move code from codebehind to behaviors. One example is if we want to scroll in selected item in a ListBox and the selected item is chosen from code, e g from a search function. The ViewModel don´t know that the view use a ListBox to show the list so it can not be used to scroll in the selected item. And we don´t want to put code in the codebehind, but if we use a behavior we solve this problem and creates a reusable component which can be used again. I have this example in my GitHub lab mention below.

There are two types of behaviors:

Attached Behavior

As seen above is the attached behavior layout and XAML code. It is really an static attached DependencyProperty which we register and then we have Get and a Set functions. This static behavior can be kind of tricky to get around if the behavior need to save some state during use. Advantage of attached behavior is that it can be used in a native windows store app. Disadvantage is that it does not show up in Blend under behaviors, and thus can not be attached if your are using Blend.

Blend Behavior

Above is blend behavior layout and XAML code. Blend behaviors inherits from Behavior<T> and uses a reference to System.Windows.Interactivity which comes from blend SDK. If you don´t find System.Windows.Interactivity in the studio you probably missing the SDK. Blend behavior shows up in Blend (it is from Blend behaviors originally comes) and can be drag and dropped for easy use. It is not static which make it saving states more easily. You can specify type (UIElement above) so you can control on which controls the behavior are used. Blend behaviors are not available in windows store apps native but the same syntax can be obtained by using some NuGet package.

Try it out

I have the code on GitHub, https://github.com/RobertHedgate/Behavior-lab-xaml. My slides from the lab are available at http://www.slideshare.net/RobertHedgate/behavior-lab-jayway but the are more or less summarized above. From GitHub you can download the code and see for yourself how it works. I have some examples of both attached and blend behaviors. If you want to do the lab yourself download BehaviorsLabStart and do the tasks:

1. Make the drag text ”dragable”. Use both Attached and Blend Behavior

2. Scroll in selected item when ”Select last item” is clicked. (To simulate supporting MVVM)

3. Make a drag and drop behavior of lists

You can of course see my solution to this in BehaviorsLabSolution.

Summary

This is a short start on how to use behaviors. Hopefully you will quickly realize that it is not hard to use either kind of behavior. They are especially useful when you need reusable code so if you need the same functionality in more than one control use a behavior. If you want to support MVVM strict and not writing any code in codebehind behaviors can solve some of those issues. To learn more on behaviors go to http://msdn.microsoft.com/en-us/library/ff726531%28v=Expression.40%29.aspx. There is also already made behaviors to download at http://wpfbehaviorlibrary.codeplex.com/.

5 Comments

  1. Sven

    Very nice! Thanks!

  2. Antony

    Good one bro..:)

  3. Cheng

    Can I use the attached behavior with two or more controls? The class seems to be a static class as well as its methods and properties. If I change that IsDigitOnlyProperty in one control, will that affect the other controls?

    • Robert Hedgate

      Yes you can use the behavior on multiplke controls and No it will not affect other controls if you change the value of one.

Trackbacks for this post

  1. Artem Grygor | Hide inactive cursor behavior.

Leave a Reply