Monday, February 14, 2011

Shared Resources

Shared resources are very useful and good saving strategy but it might cause you troubles if you don't pay attention. Changing a shared resource in one place will change all other places where it is used..

Ok this is easy and obvious but what is not obvious is that all of your resources are shared by default. Watch out. ;)

Thursday, February 10, 2011

ControlTemplate vs DataTemplate

When to use ControlTemplate and when to use DataTemplate/HierarchicalDataTemplate
Quick Answer:
- ControlTemplate is used to replace the whole visual tree of a control.
- DataTemplate/HierarchicalDataTemplate: is used only to replace the content of a control.
Example at the bottom will clarify more.

Features of each one of them:
ControlTemplate:
- you can use template-binding to bind some of the elements' properties defined in your template with the templated control's properties. Like to bind a rectangle's color in your template with the templated control's background color.
- you can use ContentPresenter to display the templated control's Content somewhere in your ControlTemplate (in case your templated control is ContentControl control).
- and you can use ItemsPresenter to display the templated control's items in case your templated control is ItemsControl control.

DataTemplate/HierarchicalDataTemplate:
- you can bind the elements you defined in your DataTemplate with properties of your corresponding Data (not control's property) classes.



Example:
if you have TreeView control and you have TreeViewItem(s) in it. The TreeViewItem is going to have some GUI in it to display the following:
- plus/minus button.
- border.
- place to display the header of the item, may be text block.
- place to display the child items of it, may be a stack.

if you use DataTemplate/HierarchicalDataTemplate, you only need to worry about displaying the text block (Content); so you don't have to worry about the plus/minus button nor to worry about the child items.

On the other hand, if you use ControlTemplate for this TreeViewItem, you will have to draw the plus/minus button and where to display the textblock (ContentPresenter) and where to display the child items (ItemsPresenter). and then you can add your own decorations around them.

I will keep updating this post as necessary. Please, let me know what you think and if you have any questions or concerns.

Wednesday, February 9, 2011

Control Template Named Parts in WPF

An important thing to remember and very nice to use: Control template named parts. Why and How?

Why this feature is important? because some controls that you create a ControlTemplate for, expect your ControlTemplate to have some elements in it that are necessary to the control's functionality.
How this feature is used? the templated control assumes that the template created for this control has some named elements. The templated control later, tries to access them and their functionality using the assumed names. So if you decided to create control template for such a control, you will need to add these required elements and give them the names that are expected by the templated control.

Unfortunately, most of the time named parts are not documented (Read the Update at the bottom please). How to identify them? you can use snoop, very simple: run your wpf application, run snoop, use snoop to browse your wpf application and inspect the visual tree of the desired control. The standard way to name these parts is by adding a prefix "PART_".

Or you develop your own snoop-like-app as suggested by Christopher Bennage in the following article:
Nice and short article: Identifying Control Templates Parts in WPF, by Christopher Bennage

I hope this helps

UPDATE : Microsoft now provides the names on this link.

HierarchicalDataTemplate not finding the specified ItemsSource

I had problem, today. My HierarchicalDataTemplate did not find the collection specified in its ItemsSource property. Well, the collection was there but the problem was that the collection was a data member; it should be a property. Case is closed