I have a treeview in WPF. I want a different color when i select the treeviewitem.
Asked
Active
Viewed 2.1k times
6
-
5I think this is more-or-less a duplicate of http://stackoverflow.com/questions/388232/ ... the same trick I've used in that answer will apply to TreeView. – Matt Hamilton May 18 '09 at 08:54
-
@MattHamilton and hevgen are both correct. Use the code sample in Matt's link. It works fine with TreeViews. OP, please accept an answer. – Riegardt Steyn Aug 28 '13 at 14:50
2 Answers
9
Simple Trigger in TreeView.ItemContainerStyle can't help for default TreeView template.
For standard template highlighting is done via background changing for specific element inside TreeView template. This specific element is not accessible for programmer without TreeView template changing. By default resource is used to set background on this element for highlighting.
So there are few ways:
- simple (but side effects possible): redefine resource with key {x:Static SystemColors.HighlightBrushKey} for TreeView or ItemsPanel template;
- Redefine complete Template for TreeView.

hevgen
- 106
- 1
- 3
-1
Try following code. It should work.
<Style TargetType="{x:Type TreeViewItem}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Yellow"/>
</Trigger>
</Style.Triggers>
</Style>

CodingNinja
- 47
- 1
- 5