This topic has not yet been rated - Rate this topic

HierarchicalDataTemplate::ItemContainerStyleSelector Property

Gets or sets custom style-selection logic for a style that can be applied to each item container.

Namespace:  System.Windows
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:
property StyleSelector^ ItemContainerStyleSelector {
	StyleSelector^ get ();
	void set (StyleSelector^ value);
}
<object>
  <object.ItemContainerStyleSelector>
    <StyleSelector .../>
  </object.ItemContainerStyleSelector>
</object>

Property Value

Type: System.Windows.Controls::StyleSelector
A StyleSelector that chooses which style to use as the ItemContainerStyle. The default is nullptr.

You use the ItemContainerStyle property to set a style to affect the appearance of the elements that contain the data items. For example, for TreeView, the generated containers are TreeViewItem controls; for Menu, they are MenuItem controls. If you have more than one style defined and need to supply logic to choose which one to apply, then you use the ItemContainerStyleSelector property instead of the ItemContainerStyle property. Note that this property is ignored if the ItemContainerStyle property is set.

The following example creates a TreeView that uses the ItemContainerStyle of a HierarchicalDataTemplate to choose between two styles for items in the second level of the TreeView.

<StackPanel Name="sp1" x:FieldModifier="public">
  <StackPanel.Resources>
    <src:TreeViewData x:Key="dataItems"/>

    <Style x:Key="TreeViewItemStyle1"  TargetType="TreeViewItem">
      <Setter Property="Foreground" Value="Navy"/>
      <Setter Property="FontStyle" Value="Italic"/>
    </Style>

    <Style x:Key="TreeViewItemStyle2"  TargetType="TreeViewItem">
      <Setter Property="Foreground" Value="Green"/>
      <Setter Property="FontWeight" Value="Bold"/>
    </Style>

    <src:TreeViewItemStyleSelector x:Key="tviSelector"/>

    <HierarchicalDataTemplate DataType="{x:Type src:ItemsForTreeView}"
                              ItemsSource="{Binding Path=SecondLevelItems}"
                              ItemContainerStyleSelector="{StaticResource tviSelector}">

      <!--Display the TopLevelName property in the first level.-->
      <TextBlock Text="{Binding Path=TopLevelName}"/>

      <!--Display each string in the SecondLevelItems property in
          the second level.-->
      <HierarchicalDataTemplate.ItemTemplate>
          <DataTemplate>
              <TextBlock Text="{Binding}"/>
          </DataTemplate>
      </HierarchicalDataTemplate.ItemTemplate>

    </HierarchicalDataTemplate>
  </StackPanel.Resources>

  <TreeView Height="200" ItemsSource="{Binding Source={StaticResource dataItems}}"
            VirtualizingStackPanel.IsVirtualizing="True">
    <TreeView.ItemContainerStyle>

      <!--Expand each TreeViewItem in the first level.-->
      <Style TargetType="TreeViewItem">
        <Setter Property="IsExpanded" Value="True"/>
      </Style>
    </TreeView.ItemContainerStyle>
  </TreeView>
</StackPanel>

The following example shows the StyleSelector that is used in the previous example.

No code example is currently available or this language may not be supported.

The following example creates the data that is used in the previous example.

No code example is currently available or this language may not be supported.

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.