HierarchicalDataTemplate.ItemContainerStyle 属性


获取或设置应用于每个子项的项容器的 StyleGets or sets the Style that is applied to the item container for each child item.

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



应用于每个子项的项容器的 StyleThe Style that is applied to the item container for each child item.


下面的示例创建一个 TreeView ,它将一个样式用于其第一个级别中的项,另一个样式用于其第二级中的项。The following example creates a TreeView that uses one style for items in its first level and another style for items in its second level. 第一级的样式是在的属性中设置的 ItemContainerStyle TreeViewThe style for the first level is set in the ItemContainerStyle property of the TreeView. 第二级的样式是在的属性中设置的 ItemContainerStyle HierarchicalDataTemplateThe style for the second level is set in the ItemContainerStyle property of the HierarchicalDataTemplate.

    <src:TreeViewData x:Key="dataItems"/>

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

      <!--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}"/>
      <!--Set the foreground of the items in the second level
          to Navy.-->
        <Style TargetType="TreeViewItem">
          <Setter Property="Foreground" Value="Navy"/>

  <TreeView Height="200" 
            ItemsSource="{Binding Source={StaticResource dataItems}}"
      <!--Expand each TreeViewItem in the first level and 
          set its foreground to Green.-->
      <Style TargetType="TreeViewItem">
        <Setter Property="IsExpanded" Value="True"/>
        <Setter Property="Foreground" Value="Green"/>

下面的示例创建了上一示例中使用的数据。The following example creates the data that is used in the previous example.

public class TreeViewData : ObservableCollection<ItemsForTreeView>

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

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

    public ObservableCollection<string> SecondLevelItems
            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()
    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


使用此属性或属性可以 ItemContainerStyleSelector 设置样式,以影响包含数据项的元素的外观。You use this property or the ItemContainerStyleSelector property to set a style to affect the appearance of the elements that contain the data items. 例如,对于 TreeView ,生成的容器是 TreeViewItem 控件; 对于 Menu ,它们是 MenuItem 控件。For example, for TreeView, the generated containers are TreeViewItem controls; for Menu, they are MenuItem controls.