Here I will post problems I and my colleagues met and solutions we found.

Thursday, February 17, 2011

The power of DataTemplate.DataType property

Somehow I overlooked the existence of DataType property from DataTemplate class. I noticed it only when I started using Microsoft Ribbon and checked how they implemented MVVM pattern I realized how convenient it can be, particularly for lists.

So, how and when to use it.
1. You want to implement MVVM pattern for you lists, which means you have some objects in your view model to support your list.
2. These are objects of different types, which means you would need to create different UI elements to support them. Like for ribbon you can use buttons, or edit boxes, or anything.
3. You use some ItemsControl UI element, which supports ItemsSource property.

Make sure that it's DataTemplate you need to change. It won't work for separators, for example, since you would need to replace Control Template for that.

No comments: