Introduction to Attached Behaviors in the context of MVVM


There is also a great and yet simple article that shows how attached behaviors is used to capture an element’s right-click event and Bind it to a MVVM ICommand property.

Introduction to Attached Behaviors in the context of MVVM

Download demo project

For example, suppose that the user searches through a TreeView for an item whose display text matches a user-defined search string. When the search logic finds a matching item, the matching ViewModel object will have its IsSelected property set to true. Then, via the magic of data binding, the TreeViewItem associated with that ViewModel object enters into the selected state (i.e., its IsSelected property is set to true, too). However, that TreeViewItem will not necessarily be in view, which means the user will not see the item that matches their search string. So how can we have the TreeViewItem brought into view when the ViewModel determines that it is in the selected state.

The ViewModel objects have no idea that a TreeViewItem exists, and is bound to them, so it does not make sense to expect the ViewModel objects to bring TreeViewItems into view. The question becomes, now, who is responsible for bringing a TreeViewItem into view when the ViewModel forces it to be selected?

Attached Behaviors

The solution to the problem explained above is to use an attached behavior. Attaching a behavior to an object simply means making the object do something that it would not do on its own.

The idea is that you set an attached property on an element so that you can gain access to the element from the class that exposes the attached property. Once that class has access to the element, it can hook events on it (e.g. TreeViewItem’s Selected event) and, in response to those events firing, make the element do things that it normally would not do. It is a very convenient alternative to creating and using subclasses, and is very XAML-friendly.

So how do we attach the behavior to every TreeViewItem in the TreeView? 
By adding a Setter to the Style applied to every TreeViewItems
  <Style TargetType="{x:Type TreeViewItem}">
    This Setter applies an attached behavior to all TreeViewItems.

Introduction to Attached Behaviors in WPF – CodeProject


One thought on “Introduction to Attached Behaviors in the context of MVVM

  1. Nice Article and was very much useful for me!Thanks.

    Im stuck up with a problem and have no idea how to progress further on it.

    I have a tree view and on selection of the same… need to load a new control… a typical preferences-settings scenario.

    Im new to WPF and MVVM any help wrt code samples / idea on implementing it would be greatly appreciated.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s