I am building a both Silverlight and WPF clients for our project.
W开发者_开发百科hen I try to assign context menus for controls, it seems to me that a context menu can be assigned only to one control at a time, is that true?
E.g. when I am drawing several lines
ContextMenu contextMenu = CreateLineMenu();
for (int i = 0; i < Points.Count - 1; i++)
{
Line l = new Line();
...
canvas.Children.Add(l);
ContextMenuService.SetContextMenu(l, contextMenu);
}
}
The menu works only for the line added as last.
A single context menu can only be assigned to one parent object at a time as it is just added into the visual tree under that child (and no visual element can have multiple parents in SL/WPF).
If you look at how context menus are defined in XAML this will become clearer.
To share one instance of a context menu you would need to dynamically assign it (e.g. when the mouse right button is clicked, or some other event).
This does seem a very inefficient way to define context menus, so I am sure others have created generic ways to share them... keep on searching (I will).
精彩评论