1

I want to set two color background to a custom tree view (wpf). You can set this propriety in a DataGrid. I could not figure out a way to do it for a tree view.

I also want to set the selection of a element to be as big as the cell.

enter image description here

<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>

Edit: When using the solution from https://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.alternationindex(v=vs.110).aspx suggested @jschroedl

enter image description here

It would be ideal to be as large as the tree view grid like shown in picture below:

enter image description here

Georgiana M
  • 323
  • 4
  • 20
  • 1
    Possible duplicate of [WPF: Alternating colors on a ItemsControl?](http://stackoverflow.com/questions/4404539/wpf-alternating-colors-on-a-itemscontrol) – MikeT Sep 27 '16 at 12:11
  • You want the tree nodes to have alternating background colors? – leetibbett Sep 27 '16 at 12:24
  • FWIW since tree view nodes can be expanded, two neighboring nodes (e.g. the last child node and the next parent node) could end up with the same background color if they are set at load time. If you try to repaint backgrounds dynamically when nodes are expanded or collapsed it could lead to some confusion for the user because of some nodes changing background colors. – leetibbett Sep 27 '16 at 12:50
  • I have noticed this while appling the solution suggested by @jschroedl. However I would still like to differentiate the nodes at least by a border. Check my edit in the question. – Georgiana M Sep 27 '16 at 12:52

1 Answers1

2

You will probably want to use ItemsControl.AlternationIndex in a Style to control how many colors to cycle (2 in your case).

MSDN Docs for ItemsControl.AlternationIndex has an example with ListBox which you can probably adapt to TreeView as well.

jschroedl
  • 4,916
  • 3
  • 31
  • 46