Reputation: 2114
How can I add in Silverlight a ContextMenu property to a UserControl (and how can I use it), if I want to add that ContextMenu just to one child-control in the UserControl?
Upvotes: 0
Views: 559
Reputation: 1036
This can easily be achieved using SL4PopupMenu available as a Nuget package or downloadable here:
http://sl4popupmenu.codeplex.com
Then all you have to do is add the following property to your UserControl:
PopupMenu _menu;
PopupMenu Menu
{
get
{
return _menu;
}
set
{
_menu = value;
_menu.AddTrigger(TriggerTypes.RightClick, ChildControl);
}
}
However there's yet another way to achieve this through selectors which the menu provides and which work much like those in jQuery. So depending on your requirements you might want to consider this path as well.
Upvotes: 1
Reputation: 189535
You need to install the Silverlight Toolkit, then you can use the ContextMenuService
.
You can add a ContextMenu
to UserControl
like this:-
<UserControl ... blah blah...>
<ContextMenuService.ContextMenu>
<MenuItem Header="First Item" Click="FirstItem_Click" />
<MenuItem Header="Second Item" Click="SecondItem_Click" />
</ContextMenuService.ContextMenu>
<Grid x:Name="LayoutRoot"> </Grid>
</UserControl>
You simple have the click event handlers in your code behind on the UserControl to respond to menu item selection.
If are using MVVM then MenuItem
also has a Command
property the you can bind to.
You can add this ContextMenuService.ContextMenu
attached property to any framework element inside the UserControl if you want to provide specific menus for specific areas of the UI.
Upvotes: 2