首先下载一下并且安装Silverlight_4_Toolkit_April_2010.msi(支持vs2010以上版本哦),下载Silverlight_5_Toolkit_December_2011.msi也可以的
xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <Grid Background="White " MouseRightButtonDown="grid_mouseright_down"> <Button Content="右键菜单" Height="23" HorizontalAlignment="Left" Margin="10,10,0" Name="btnRight" VerticalAlignment="Top" Width="75" /> </Grid>
cs文件
public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); createMenu(); } private void grid_mouseright_down(object sender,MouseButtonEventArgs e) { e.Handled = true; } private void createMenu() { ContextMenu conm=new ContextMenu(); MenuItem mitem=new MenuItem(); mitem.Header = "hello world"; mitem.Click += MenuItem_Click;//为菜单项注册事件 conm.Items.Add(mitem); ContextMenuService.SetContextMenu(btnRight,conm); } private void MenuItem_Click(object sender,RoutedEventArgs e) { MessageBox.Show("右键菜单事件"); } }
效果图如下: