Friday, November 5, 2010

WPF ContentControl and ItemsControl concepts

ContentControl is used to display a control and data from a single object.
ContentControl extends Control and has three main properties (of which 'Template' property is inherited from the parent Control class):

        // Summary:
        //     Gets or sets the value of the System.Windows.Controls.ContentControl dependency
        //     property.
        //
        // Returns:
        //     An object that contains the control's content. The default is null.
        public object Content { getset; }
        //
        // Summary:
        //     Gets or sets the data template that is used to display the content of the
        //     System.Windows.Controls.ContentControl.
        //
        // Returns:
        //     The data template that is used to display the content of the System.Windows.Controls.ContentControl.
        public DataTemplate ContentTemplate { getset; }
        //(Note: Inherited from parent 'Control' class
        // Summary:
        //     Gets or sets a control template.
        //
        // Returns:
        //     The template that defines the appearance of the System.Windows.Controls.Control.
        public ControlTemplate Template { getset; }
ControlTemplate is used for display of the control. All ContentControl objects come with default values for their ControlTemplate property. 
The control author can define the default ControlTemplate and the application author can override the ControlTemplate to reconstruct the visual structure of the control.
The default ControlTemplate contains a ContentPresenter that is defined by the control author.  If the application author chooses to redefine the Template property, he HAS to include the ContentPresenter in the ControlTemplate otherwise, the ContentControl will not display.
The DataTemplate is used to display the data contained in the control.This should be defined in XAML only and cannot be done in code.
The XAML usage that defines the content for creating a data template is not exposed as a settable property. It is special behavior built into the XAML processing of a DataTemplate object element.
Here is how they all work together (click here for more details):
You typically use the ContentPresenter in the ControlTemplate of a ContentControl to specify where the content is to be added. Every ContentControl type has a ContentPresenter in its default ControlTemplate.
When a ContentPresenter object is in a ControlTemplate of a ContentControl, the ContentContentTemplate, and ContentTemplateSelector properties get their values from the properties of the same names of theContentControl. You can have the ContentPresenter property get the values of these properties from other properties of the templated parent by setting the ContentSource property or binding to them.
The ContentPresenter uses the following logic to display the Content:
Similarly, The ItemsControl is used for collections. The equivalent of ContentTemplate here is ItemTemplate (hence the type is the same: DataTemplate). In an items control, when bound to a collection, each element of the collection is displayed using the ItemTemplate.
        //
        // Summary:
        //     Gets or sets the System.Windows.DataTemplate used to display each item.
        //
        // Returns:
        //     The template that specifies the visualization of the data objects. The default
        //     is null.
        public DataTemplate ItemTemplate { getset; }