I am trying to change a TreeViewItem background color when it selected but lost focus. I saw some similar questions like: WPF TreeViewItem Background , but I couldn\'t use it...
In default template of TreeViewItem
, there is MultiDataTrigger which set the color to ControlBrushKey
when item is not in focus which you can override in your TreeView resources like this:
then there is no need to override ControlTemplate of TreeViewItem.
But, however it also set text of treeViewItem to ControlTextBrushKey
which by default is black. So, with only ControlBrushKey
overriden, you will get it work like this:
Without Focus:
With Focus
As you can see brush is over-ridden properly but if you override ControlTextBrushKey
and set it to white than treeView Item's won't be visible since controlTextBrush will be set to white.
However, in case you want exact same look of TreeViewItem like it has with focus, you have to override control template and remove that MultiDataTrigger from the default template.
Default template will go like this: