I have been having problems with this for some time now, and have come up with some less-than-desirable solutions. The problem is that when a TreeViewItem’s context menu is opened, the TreeViewItem is greyed out. Is it possible for a TreeViewItem to stay highlighted while its ContextMenu is open?
The problem with the TreeViewItem greying out, is that it gives no relation to the context menu and the TreeViewItem, and it looks ugly.
Generally, the code I use for setting a context menu is this. Sometimes the context menu will be generated by the code with a PreviewRightMouseButtonDown EventSetter, but it doesn’t make a difference:
<TreeView>
<TreeView.Resources>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Menu Item 1" />
<MenuItem Header="Menu Item 2" />
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</TreeView.Resources>
<TreeViewItem Header="Item 1">
<TreeViewItem Header="Sub-Item 1"/>
</TreeViewItem>
<TreeViewItem Header="Item 2"></TreeViewItem>
</TreeView>
So far the only solution I have found is to override the “grey” unfocused color with the focused color, but then the TreeView never seems unfocused, such as when another control is clicked on. I have had problems with ListViews as well.
WPF’s default behavior is to change the TreeViewItem to gray when the ContextMenu opens, but like virtually everything else in WPF you can override this:
Here’s the attached property:
Here’s the setter in the style:
Here’s the trigger:
How it works: Every time the ContextMenu opens its IsOpen property is set. The binding causes your attached property to be set on the TreeViewItem. This, combined with IsSelected, invokes the trigger which changes the foreground and background colors to make the item still appear selected.