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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>