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


Understand your code using Dependency Graphs, Sequence Diagrams and the Architecture Explorer


Have you ever had to work with a legacy codebase?  Designing new functionality on existing applications can be daunting.  There are always differences between the original design and the current implementation.  The new Architecture tools within Visual Studio 2010 help you to understand the application you have, design new functionality you need, and validate that your design and your implementation do not deviate.  In this post, we’ll look at how to understand the application that you have. 


Create a Silverlight Client using OData and Bing Map


Part 1

Part 2 – turn into a Windows Phone 7 Client


In summary, this is what we’ll be doing now.

  1. Creating a Silverlight project
  2. See how to consume oData in Silverlight
  3. Use NerdDinner oData endpoint to pull Dinners and RSVPs from our Silverlight client
  4. Show them in a Bing map using Bing’s Silverlight Map SDK by geo-coding the addresses
  5. Enable our Silverlight application to run Out of Browser

We’ll be using the following technology stack. All these downloads are free for you.

7 Great Principles for Dealing with Haters


  1. It doesn’t matter how many people don’t get it. What matters is how many people do.
  2. 10% of people will find a way to take anything personally. Expect it.
  3. “Trying to get everyone to like you is a sign of mediocrity.” (Colin Powell)
  4. “If you are really effective at what you do, 95% of the things said about you will be negative.” (Scott Boras)
  5. “If you want to improve, be content to be thought foolish and stupid.” (Epictetus)
  6. “Living well is the best revenge.” (George Herbert)
  7. Keep calm and carry on.

Via Tim Ferriss: 7 Great Principles for Dealing with Haters