Lester's WPF\SL Blog

Simple, Easy & Beautiful

Fix for Opening dynamically created context menus on ListviewItems

Fix for Opening dynamically created context menus on ListviewItems

Rate This
  • Comments 2

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

....

</Setter>

</Style>

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 :)

  • How do you set the ContextMenu to the ListViewItem

  • its a style so it gets automatically applied

Page 1 of 1 (2 items)