This documentation is archived and is not being maintained.

HierarchicalDataTemplate.ItemContainerStyleSelector Property

Updated: July 2008

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)

Public Property ItemContainerStyleSelector As StyleSelector
Dim instance As HierarchicalDataTemplate 
Dim value As StyleSelector 

value = instance.ItemContainerStyleSelector

instance.ItemContainerStyleSelector = value
    <StyleSelector .../>

Property Value

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

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">
    <src:TreeViewData x:Key="dataItems"/>

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

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

    <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.-->
          <TextBlock Text="{Binding}"/>


  <TreeView Height="200" ItemsSource="{Binding Source={StaticResource dataItems}}"

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

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

Public Class TreeViewItemStyleSelector
    Inherits StyleSelector
    Public Overloads Overrides Function SelectStyle(ByVal item As Object, ByVal container As DependencyObject) As Style
        Dim itemString As String = TryCast(item, String)

        Dim strings As String() = itemString.Split(Nothing)
        Dim value As Integer 

        If Not Int32.TryParse(strings(strings.Length - 1), value) Then 
            Return Nothing 
        End If 

        Dim win1 As Window1 = CType(Application.Current.MainWindow, Window1)
        Dim sp As StackPanel = win1.sp1

        If value < 5 Then 
            Return TryCast(sp.FindResource("TreeViewItemStyle1"), Style)
            Return TryCast(sp.FindResource("TreeViewItemStyle2"), Style)
        End If 

    End Function 
End Class

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

Public Class TreeViewData
    Inherits ObservableCollection(Of ItemsForTreeView)

    Public Sub New()
        For i As Integer = 0 To 99
            Dim item As New ItemsForTreeView()
            item.TopLevelName = "item " & i.ToString()
    End Sub 
End Class 

Public Class ItemsForTreeView
    Private _TopLevelName As String 
    Public Property TopLevelName() As String 
            Return _TopLevelName
        End Get 
        Set(ByVal value As String)
            _TopLevelName = value
        End Set 
    End Property 
    Private level2Items As ObservableCollection(Of String)

    Public ReadOnly Property SecondLevelItems() As ObservableCollection(Of String)
            If level2Items Is Nothing Then
                level2Items = New ObservableCollection(Of String)()
            End If 
            Return level2Items
        End Get 
    End Property 

    Public Sub New()
        For i As Integer = 0 To 9
            SecondLevelItems.Add("Second Level " & i.ToString())
    End Sub 
End Class

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

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

.NET Framework

Supported in: 3.5 SP1, 3.0 SP2




July 2008

Added topic for new member.

SP1 feature change.