Skip to main content
.NET Framework Class Library
ContentControlContentTemplateSelector Property

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

Namespace:   System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
Syntax
<[%$TOPIC/ms592478_en-us_VS_110_1_0_0_0_0%](True)> _
Public Property ContentTemplateSelector As [%$TOPIC/ms592478_en-us_VS_110_1_0_0_0_1%]
[[%$TOPIC/ms592478_en-us_VS_110_1_0_1_0_0%](true)]
public [%$TOPIC/ms592478_en-us_VS_110_1_0_1_0_1%] ContentTemplateSelector { get; set; }
[[%$TOPIC/ms592478_en-us_VS_110_1_0_2_0_0%](true)]
public:
property [%$TOPIC/ms592478_en-us_VS_110_1_0_2_0_1%]^ ContentTemplateSelector {
	[%$TOPIC/ms592478_en-us_VS_110_1_0_2_0_2%]^ get ();
	void set ([%$TOPIC/ms592478_en-us_VS_110_1_0_2_0_3%]^ value);
}
[<[%$TOPIC/ms592478_en-us_VS_110_1_0_3_0_0%](true)>]
member ContentTemplateSelector : [%$TOPIC/ms592478_en-us_VS_110_1_0_3_0_1%] with get, set
<object>
  <object.ContentTemplateSelector>
    <[%$TOPIC/ms592478_en-us_VS_110_1_0_4_0_0%] .../>
  </object.ContentTemplateSelector>
</object>
<object ContentTemplateSelector="{ResourceExtension TemplateSelectorKey}"/>

XAML Values

ResourceExtension

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

TemplateSelectorKey

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

Property Value

Type: System.Windows.ControlsDataTemplateSelector
A data template selector. The default value is .
Dependency Property Information

Identifier field

ContentTemplateSelectorProperty

Metadata properties set to true

None

Remarks

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.

Examples

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.

Public Class NumderDataTemplateSelector
    Inherits DataTemplateSelector

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

        Dim numberStr As String = item '

        If Not (numberStr Is Nothing) Then 
            Dim num As Integer 
            Dim win As Window = Application.Current.MainWindow

            Try
                num = Convert.ToInt32(numberStr)
            Catch 
                Return Nothing 
            End Try 

            ' Select one of the DataTemplate objects, based on the  
            ' value of the selected item in the ComboBox. 
            If num < 5 Then 
                Return win.FindResource("numberTemplate") '

            Else 
                Return win.FindResource("largeNumberTemplate") '
            End If 
        End If 

        Return Nothing 

    End Function 'SelectTemplate
End Class 'NumderDataTemplateSelector 
public class NumderDataTemplateSelector : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        string numberStr = item as string;

        if (numberStr != null)
        {
            int num;
            Window win = Application.Current.MainWindow;

            try
            {
                num = Convert.ToInt32(numberStr);
            }
            catch
            {
                return null;
            }

            // Select one of the DataTemplate objects, based on the  
            // value of the selected item in the ComboBox. 
            if (num < 5)
            {
                return win.FindResource("numberTemplate") as DataTemplate;
            }
            else
            {
                return win.FindResource("largeNumberTemplate") as DataTemplate;

            }
        }

        return null;
    }

}
<Window.Resources>

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

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

  <local:NumderDataTemplateSelector x:Key="numberTemplateSelector"/>

</Window.Resources>
<StackPanel>

  <!--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}">
  </Label>

  <ComboBox Name="numberList">
    <ComboBoxItem>1</ComboBoxItem>
    <ComboBoxItem>2</ComboBoxItem>
    <ComboBoxItem>3</ComboBoxItem>
    <ComboBoxItem>4</ComboBoxItem>
    <ComboBoxItem>5</ComboBoxItem>
    <ComboBoxItem>6</ComboBoxItem>
    <ComboBoxItem IsSelected="True">7</ComboBoxItem>
    <ComboBoxItem>8</ComboBoxItem>
    <ComboBoxItem>9</ComboBoxItem>
    <ComboBoxItem>10</ComboBoxItem>
  </ComboBox>

</StackPanel>
Version Information

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.