I created a simple, stripped ListView style that highlights an element when the IsMouseOver property is true. This is done by triggering in the ItemContainerStyle. This works great and the xaml is like this:
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<!--UserControl with actual content goes here-->
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<!--here is a border with the ContentPresenter inside-->
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Lime"/>
</Trigger>
</Style.Triggers>
</Style>
<ListView.ItemContainerStyle>
</ListView>
However I would also like that the color set on hovering stays when the actual element’s contextmenu is shown by right-clicking it. Basically the question is like this one, except that I cannot use the (otherwise great) answer there: the idea is to add a trigger to check when the contextmenu is open:
<DataTrigger Binding="{Binding ContextMenu.IsOpen}" Value="True">
<Setter Property="Background" Value="Lime"/>
</DataTrigger>
The question is: what binding expression do I enter in order to figure out that ContextMenu.IsOpen on the actual content set in the DataTemplate? I tried all sort of things like referring to ContentPresenter.ContextMenu.IsOpen etc but none worked.
Apart from using ContextMenu.IsOpen, I already tried tons of combinations of triggers on IsSelected, event triggers on MouseLeave etc but also to no avail. So the second question is: if the contextmenu trick does not work, is there another way to get this effect? Basically I want a list view that does not support selecting of any kind, but does show the user at which element the mouse is, no matter is a menu is partly hiding it or not.
As uausal when something seems hard in xaml, this was perfectly solvable using attached properties with the additional bonus of being reuasable. The basic principle is to attach a behavior to a FrameworkElement, and hook it’s
MouseEneter/Leaveevents. Apart from those, also look for any children having a contextmenu and hook theContextMenuOpening/Closingevents. I don’t have a blog or repository so here is the code, I imagine this can be useful for others as well.And in xaml: