I am seeing some strange behaviour for wpf ContextMenu. What I am trying to achieve is to have a dynamic ContextMenu generated each time user do a right click on the map. However, there's seems to be some cache mechanism on the ConexteMenu or the map click event. The first time user right click on the map, nothing happens - no ConextMenu is popping up. The second right click will bring up the correct ConextMenu. The third and later right click will bring back the same ContextMenu as teh second click - not updated after the content of the ContextMenu has been updated to show different items.
Is this a known bug and is there a fix or alternative way to implemente a dynamic ContextMenu?
Below is a sample code I have put together to demostrate the strange behaviour.
public partial class Window1 : Window
{
int rightClickCount = 0;
public Window1()
{
InitializeComponent();
}
private void WpfMap_Loaded(object sender, RoutedEventArgs e)
{
wpfMap1.MapUnit = GeographyUnit.DecimalDegree;
wpfMap1.CurrentExtent = new RectangleShape(-155.733, 95.60, 104.42, -81.9);
WorldMapKitWmsWpfOverlay worldOverlay = new WorldMapKitWmsWpfOverlay();
wpfMap1.Overlays.Add("WMK", worldOverlay);
wpfMap1.MapClick += new EventHandler<mapclickwpfmapeventargs>(wpfMap1_MapClick);</mapclickwpfmapeventargs>
wpfMap1.Refresh();
}
private void wpfMap1_MapClick(object sender, MapClickWpfMapEventArgs e)
{
if (e.MouseButton != MapMouseButton.Right)
return;
ContextMenu cm = new ContextMenu();
MenuItem mi = new MenuItem();
mi.Header = (++ rightClickCount).ToString();
cm.Items.Add( mi );
wpfMap1.ContextMenu = cm;
}
}