簡體   English   中英

如何將2色背景設置為TreeView(WPF)

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM