HierarchicalDataTemplate.ItemContainerStyleSelector 属性

定义

获取或设置可应用于每个项容器的样式的自定义样式选择逻辑。

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

属性值

StyleSelector

一个 StyleSelector,用来选择将哪种样式用作 ItemContainerStyle。 默认值为 null

示例

以下示例创建一个 TreeView ,该示例使用 ItemContainerStyle a HierarchicalDataTemplate 在二级 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>

以下示例演示 StyleSelector 了上一个示例中所使用的示例。

public class TreeViewItemStyleSelector : StyleSelector
{
    public override Style SelectStyle(object item, DependencyObject container)
    {
        string itemString = item as string;

        string[] strings = itemString.Split(null);
        int value;
        
        if (!Int32.TryParse(strings[strings.Length - 1], out value))
        {
            return null;
        }

        StackPanel sp = ((Window1) Application.Current.MainWindow).sp1;

        if (value < 5)
        {
            return sp.FindResource("TreeViewItemStyle1") as Style;
        }
        else
        {
            return sp.FindResource("TreeViewItemStyle2") as Style;
        }
    }
}
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)
        Else
            Return TryCast(sp.FindResource("TreeViewItemStyle2"), Style)
        End If

    End Function
End Class

以下示例创建在上一个示例中使用的数据。

public class TreeViewData : ObservableCollection<ItemsForTreeView>
{

    public TreeViewData()
    {
        for (int i = 0; i < 100; ++i)
        {
            ItemsForTreeView item = new ItemsForTreeView();
            item.TopLevelName = "item " + i.ToString();
            Add(item);
        }
    }
}

public class ItemsForTreeView
{
    public string TopLevelName { get; set; }
    private ObservableCollection<string> level2Items;

    public ObservableCollection<string> SecondLevelItems
    {
        get 
        {
            level2Items ??= new ObservableCollection<string>();
            return level2Items;
        }
    }

    public ItemsForTreeView()
    {
        for (int i = 0; i < 10; ++i)
        {
            SecondLevelItems.Add("Second Level " + i.ToString());
        }
    }
}
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()
            Add(item)
        Next
    End Sub
End Class


Public Class ItemsForTreeView
    Private _TopLevelName As String
    Public Property TopLevelName() As String
        Get
            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)
        Get
            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())
        Next
    End Sub
End Class

注解

使用 ItemContainerStyle 属性设置样式以影响包含数据项的元素的外观。 例如,生成的 TreeView容器是 TreeViewItem 控件;对于 Menu,它们是 MenuItem 控件。 如果定义了多个样式,并且需要提供逻辑来选择要应用的样式,则使用 ItemContainerStyleSelector 属性而不是 ItemContainerStyle 属性。 请注意,如果设置了此属性, ItemContainerStyle 则忽略此属性。

适用于