Notes on Software

Posts Tagged ‘DataTemplate’

Differences between WPF and Silverlight – 1

Posted by K. M. on February 25, 2011

This is the first post in a series documenting the differences between WPF (v3.5+) and Silverlight (v4).

DataTemplates in an ItemsControl in bound mode (when ItemsSource is set):

An ItemsControl generates a container for each item in the ItemsSource. The container typically displays the item via a ContentPresenter (either directly or through a ControlTemplate for the container).

WPF:

The logic in the PrepareContainerForItemOverride method sets the ContentTemplate of the container to the ItemTemplate property if it is not null and the ContentTemplateSelector to the ItemTemplateSelector if it is not null. Additionally, the ItemContainerStyle gets applied to the container. This results in the following order of precedence for the data template

  1. ItemsControl.ItemTemplate
  2. ContentTemplate from ItemsControl.ItemContainerStyle
  3. ItemsControl.ItemTemplateSelector
  4. ContentTemplateSelector from ItemsControl.ItemContainerStyle

Silverlight:

There is no DataTemplateSelector. Also the logic in the PrepareContainerForItemOverride method sets the ContentTemplate of the container to the ItemTemplate property even if it is null. This results in the ItemsControl always using the ItemTemplate property for the data template. The ContentTemplate from ItemsControl.ItemContainerStyle is always ignored.

My comments:

The silverlight implementation is broken and fixing it will mean a breaking change. So I doubt if it will ever be fixed. It seems that the developers who implemented this either did not understand the concept of dependency property precedence or did not care to compare the implementation with the WPF one. Bad.

Posted in .NET, Silverlight, WPF | Tagged: , , , | Leave a Comment »