28

I have a treeview structure. When I try to click on the nodes there is a blue color that shows the node selected. How can I remove that. I don't want a selection color to be displayed on the tree.

HXD
  • 506
  • 1
  • 7
  • 26
  • possible duplicate of [How do I highlight a treeview selected item with some color?](http://stackoverflow.com/questions/876759/how-do-i-highlight-a-treeview-selected-item-with-some-color) – Cédric Bignon Jul 23 '13 at 15:21

1 Answers1

77

ItemContainerStyle method does not work for me say on Windows-8. There are 4 brushes that generally correspond to this and are used by the default Template for TreeViewItem

keys:

HighlightBrushKey - Background with focus.

HighlightTextBrushKey - Foreground with focus.

InactiveSelectionHighlightBrushKey - Background without focus.

InactiveSelectionHighlightTextBrushKey - Foreground without focus.

Just override them as you see fit, for your requirement something like this would do fine:

<TreeView>
  <TreeView.Resources>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                      Color="Transparent" />
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}"
                      Color="Black" />
    <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
                      Color="Transparent" />
    <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}"
                      Color="Black" />
  </TreeView.Resources>
</TreeView>

Do pay attention to only overriding them within the scope you require. For example if you put all this into App.xaml you're going to see some weird side-effects as all control's using these Brushes would now end up using your overridden ones which may not be what you're after.

Viv
  • 17,170
  • 4
  • 51
  • 71
  • 4
    Please note that `SystemColors.InactiveSelectionHighlightBrushKey` and `SystemColors.InactiveSelectionHighlightTextBrushKey` is only for .NET 4.5 or later. – bitbonk Nov 20 '14 at 16:02