Fix for Opening dynamically created context menus on ListviewItems

First of all, a big thanks to all your feature requests on my previous post... 🙂 .. You can still post your comments/feature requests...

Listview is a nice control to use and I have been styling this control to get some crazy results. One of the glitches I came across (and was also noticed by some of the readers) was opening the dynamically created context menus for the Listview items. My code looks something like this

<Style TargetType="{x:Type ListViewItem}">

<EventSetter Event="ContextMenuOpening" Handler="OpenContextMenu"/>
//open context menu creates the menu




The glitch comes on right clicking the item... You see the menu on performing the right click the second time. hmmm... It turns out that since the ContextMenu is not set the framework skips the opening part. The fix here is simple - assign the context menu to be empty initially.

<ContextMenu x:Key="EmptyContextMenu"/>
Setter Property="ContextMenu" Value="{StaticResource EmptyContextMenu}"/>

Not a major bug but its always nice to know the fix 🙂

Comments (2)

  1. pusch says:

    How do you set the ContextMenu to the ListViewItem

  2. its a style so it gets automatically applied

Skip to main content