ContentControl.ContentTemplateSelector Property


Gets or sets a template selector that enables an application writer to provide custom template-selection logic.

 property System::Windows::Controls::DataTemplateSelector ^ ContentTemplateSelector { System::Windows::Controls::DataTemplateSelector ^ get(); void set(System::Windows::Controls::DataTemplateSelector ^ value); };
public System.Windows.Controls.DataTemplateSelector ContentTemplateSelector { get; set; }
member this.ContentTemplateSelector : System.Windows.Controls.DataTemplateSelector with get, set
Public Property ContentTemplateSelector As DataTemplateSelector

Property Value


A data template selector. The default value is null.



The following example shows how to use the ContentTemplateSelector property. This example binds the selected item in a ComboBox to the Content property of a Label, which inherits from ContentControl. When the user selects a value below 5, the value of the selected item appears in a black square in the Label. When the user selects a value that is 5 or above, the value appears in a green ellipse. The example accomplishes this by creating two DataTemplate objects and a DataTemplateSelector, which is set to the ContentTemplateSelector property and chooses the appropriate DataTemplate based on the value of the selected item.


  <!--Create two DataTemplate objects to be 
  selected by the DataTemplateSelector.-->
  <DataTemplate x:Key="numberTemplate">
      <Rectangle Stroke="Black" />
      <TextBlock Margin="5" Text="{Binding}" FontSize="18"/>

  <DataTemplate x:Key="largeNumberTemplate">
      <Ellipse Stroke="Green" StrokeThickness="4"/>
      <TextBlock Margin="10" Text="{Binding}" FontSize="24" 
                 Foreground="Red" FontWeight="Bold" />

  <local:NumberDataTemplateSelector x:Key="numberTemplateSelector"
                                    NumberTemplate="{StaticResource numberTemplate}"
                                    LargeNumberTemplate="{StaticResource largeNumberTemplate}"/>


  <!--Bind the content of the Label to the selected item 
  in the ComboBox.-->
  <Label  Foreground="Black"
          Content="{Binding ElementName=numberList, Path=SelectedItem.Content}"
          ContentTemplateSelector="{StaticResource numberTemplateSelector}">

  <ComboBox Name="numberList">
    <ComboBoxItem IsSelected="True">7</ComboBoxItem>

public class NumberDataTemplateSelector : DataTemplateSelector
    public DataTemplate NumberTemplate { get; set; }
    public DataTemplate LargeNumberTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
        // Null value can be passed by IDE designer
        if (item == null) return null;

        var num = Convert.ToInt32((string)item);
        // Select one of the DataTemplate objects, based on the 
        // value of the selected item in the ComboBox.
        if (num < 5)
            return NumberTemplate;
            return LargeNumberTemplate;
Public Class NumberDataTemplateSelector
    Inherits DataTemplateSelector

    Public Property NumberTemplate As DataTemplate
    Public Property LargeNumberTemplate As DataTemplate

    Public Overrides Function SelectTemplate(ByVal item As Object, _
                    ByVal container As DependencyObject) As DataTemplate

        ' Nothing can be passed by IDE designer
        if (item Is Nothing) Then
            Return Nothing
        End If

        Dim num = Convert.ToInt32(CStr(item))

        ' Select one of the DataTemplate objects, based on the 
        ' value of the selected item in the ComboBox.
        If num < 5 Then
            Return NumberTemplate

            Return LargeNumberTemplate
        End If

    End Function 'SelectTemplate
End Class


Typically, you create a DataTemplateSelector when you have more than one DataTemplate for the same type of objects and you want to supply your own logic to choose a DataTemplate to apply based on the properties of each data object. Note that if you have objects of different types you can set the DataType property on the DataTemplate. If you do that, then there is no need to create a DataTemplateSelector. Furthermore, if you have objects of the same type but with different properties, you can also consider using a DataTrigger or a data converter. For more information, see Data Templating Overview.

To create a template selector, create a class that inherits from DataTemplateSelector and override the SelectTemplate method. After your class is defined, you can assign an instance of the class to the template selector property of your element.

If both the ContentTemplateSelector and the ContentTemplate properties are set, then this property is ignored.

XAML Attribute Usage

<object ContentTemplateSelector="{ResourceExtension TemplateSelectorKey}"/>  

XAML Values

A markup extension that identifies how to reference the template resource, either StaticResource or DynamicResource. See XAML Resources.

The key that identifies the requested template selector. The key refers to an existing resource in a ResourceDictionary.

Dependency Property Information

Identifier field ContentTemplateSelectorProperty
Metadata properties set to true None

Applies to