Change highlight color in hierarchical TreeView

flurreh picture flurreh · Apr 16, 2010 · Viewed 10.3k times · Source

I have got a TreeView with a HierarchicalDataTemplate.

<HierarchicalDataTemplate x:Key="treeViewItemTemplate" ItemsSource="{Binding GetChildren}">
    <DockPanel Margin="0,8,8,0">
        <Image Source="{Binding GetImage}" Width="16" Height="16" />
        <local:MonitorTriStateCheckBox Margin="4,0,0,0" IsChecked="{Binding IsChecked}" Click="CheckBox_Clicked" Tag="{Binding UniqueKey}" Style="{DynamicResource CheckBox}"></local:MonitorTriStateCheckBox>
        <TextBlock Margin="4,0,0,0" Text="{Binding Name}" Style="{DynamicResource TextBlock}">
        </TextBlock>
    </DockPanel>
    <HierarchicalDataTemplate.Triggers>
        <Trigger Property="TreeViewItem.IsSelected" Value="True">
            <Setter Property="TreeViewItem.Background" Value="Orange" />
        </Trigger>
    </HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>

As you can see in the code, i set the is selected Trigger of the TreeViewItem, but this has no effect. I alos tried this:

<TreeView.ItemContainerStyle>
    <Style TargetType="{x:Type TreeViewItem}">
        <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
        <Setter Property="Visibility" Value="{Binding IsVisible, Mode=TwoWay}" />
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="Orange" />
            </Trigger>
        </Style.Triggers>
    </Style>
</TreeView.ItemContainerStyle>

But that had no effect either.

Has anyone got an idea what to do, to change the hightlight color of a TreeViewItem?

Answer

Olga picture Olga · Jul 21, 2010

Try this...

<TreeView.Resources>                                                
    <SolidColorBrush Color="Green" x:Key="{x:Static SystemColors.HighlightBrushKey}"/>
</TreeView.Resources>