Export (0) Print
Expand All

TabControl.ContentTemplateSelector Property

Gets or sets a DataTemplateSelector that provides custom logic for choosing the template that is used to display the content of the control.

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

public DataTemplateSelector ContentTemplateSelector { get; set; }
<object>
  <object.ContentTemplateSelector>
    <MyDataTemplateSelectorImplementation .../>
  </object.ContentTemplateSelector>
</object>
<object ContentTemplateSelector="{ResourceExtension DataTemplateSelectorClassKey}"/>

XAML Values

ResourceExtension

One of the following: StaticResource, or DynamicResource. See XAML Resources.

DataTemplateSelectorClassKey

The key that identifies the selector implementation being requested. The key refers to a derived class that implements a practical SelectTemplate override. For information about how to map your custom class, see XAML Namespaces and Namespace Mapping for WPF XAML. You can also programmatically add an instance of your DataTemplateSelector class as a resource to a resource dictionary.

MyDataTemplateSelectorImplementation

A class derived from DataTemplateSelector and overrides SelectTemplate. For information about how to map your custom class, see XAML Namespaces and Namespace Mapping for WPF XAML.

Property Value

Type: System.Windows.Controls.DataTemplateSelector
A ContentTemplateSelector. The default is null.

Identifier field

ContentTemplateSelectorProperty

Metadata properties set to true

None

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.

The following example uses the ContentTemplateSelector property to display the content of a TabItem differently, depending on the hometown of the selected person.

<DataTemplate x:Key="DetailTemplate">
  <Border Width="300" Height="100" Margin="20"
          BorderBrush="Aqua" BorderThickness="1" Padding="8">
    <Grid>
      <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100"/>
        <ColumnDefinition/>
      </Grid.ColumnDefinitions>
      <TextBlock Grid.Row="0" Grid.Column="0" Text="First Name:"/>
      <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=FirstName}"/>
      <TextBlock Grid.Row="1" Grid.Column="0" Text="Last Name:"/>
      <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=LastName}"/>
      <TextBlock Grid.Row="2" Grid.Column="0" Text="Home Town:"/>
      <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding Path=HomeTown}"/>
    </Grid>
  </Border>
</DataTemplate>

<DataTemplate x:Key="SeattleTemplate">
  <DataTemplate.Resources>
    <Style TargetType="TextBlock">
      <Setter Property="FontSize" Value="16"/>
    </Style>
  </DataTemplate.Resources>
  <Border Width="300" Height="100" Margin="20"
          BorderBrush="DarkSeaGreen" BorderThickness="3" Padding="15">
    <Grid>
      <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="120"/>
        <ColumnDefinition/>
      </Grid.ColumnDefinitions>
      <TextBlock Grid.Row="0" Grid.Column="0" Text="Please welcome"/>
      <DockPanel Grid.Row="0" Grid.Column="1" >
        <TextBlock Text="{Binding Path=FirstName}"/>
        <TextBlock Text=" "/>
        <TextBlock Text="{Binding Path=LastName}"/>
      </DockPanel>
      <TextBlock Grid.Row="1" Grid.ColumnSpan="2" Text="from the Emerald City!"/>
    </Grid>
  </Border>
</DataTemplate>


...


<!--Bind to the collection MyFriends and use a DataTemplateSelector
to specify the appearance of the content.-->
<TabControl Name="tabCtrl1" Width="400" Height="200" 
         ItemsSource="{Binding Source={StaticResource MyFriends}}"
         ContentTemplateSelector="{StaticResource PersonSelector}"/>
public class PersonTemplateSelector : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {

        // The content of each TabItem is a Person object. 
        if (item is Person)
        {
            Person person = item as Person;

            Window win = Application.Current.MainWindow;

            // Select one of the DataTemplate objects, based on the  
            // person's home town. 
            if (person.HomeTown == "Seattle")
            {
                return win.FindResource("SeattleTemplate") as DataTemplate;
            }
            else
            {
                return win.FindResource("DetailTemplate") as DataTemplate;

            }
        }

        return null;
    }

}

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.

Show:
© 2014 Microsoft