![](/img/trans.png)
[英]How to change the background color of TreeViewItem when clicking in the WPF treeview?
[英]How to set 2 color background to a TreeView (WPF)
我想將兩種顏色的背景設置為自定義樹視圖(wpf)。 您可以在DataGrid中設置此屬性。 我無法找到一種用於樹狀視圖的方法。
我還想將元素的選擇設置為與單元格一樣大。
<TreeView Grid.Row="2" Name="TreeView" DataContext="{Binding Path=TreeModel}" ItemsSource="{Binding TreeItems}" SelectedItemChanged="TreeView_OnSelectedItemChanged">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:NodeViewModel}" ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal">
<Label Content="{Binding NameNode}"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Add" Command="{Binding AddMachinePart_Command}"/>
<MenuItem Header="Remove" Command="{Binding RemoveMachinePart_Command}" IsEnabled="{Binding IsModule}"/>
<MenuItem Header="Edit" Command="{Binding EditMachinePart_Command}" IsEnabled="{Binding IsModule}"/>
<Separator></Separator>
<MenuItem Header="Copy path" Command="{Binding CopyPath_Command}" IsEnabled="{Binding IsModule}"></MenuItem>
</ContextMenu>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold" />
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
編輯:從https://msdn.microsoft.com/zh-cn/library/system.windows.controls.itemscontrol.alternationindex(v=vs.110).aspx使用解決方案時,建議使用@jschroedl
最好與樹狀視圖網格一樣大,如下圖所示:
您可能需要在Style
使用ItemsControl.AlternationIndex
來控制要循環的顏色(在您的情況下為2種)。
MSDN Docs的ItemsControl.AlternationIndex帶有ListBox
的示例,您可能也可以使其適應TreeView
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.