1、在MainWindow.xaml添加Window.Resources
- Window.Resources>
- ContextMenu x:Key ="rightKeyMenu">
- MenuItem Header="人员资料" Name="ItemUser" />
- MenuItem Header="选项" Name ="ItemDrawOption" />
- Separator />
- MenuItem Header="关闭" Name="ItemClose" />
- /ContextMenu>
- /Window.Resources>
2、在大纲视图中选中对应Item,设置并编辑其click事件
- private void ItemUser_Click(object sender, RoutedEventArgs e)
- {
- User myUser = new User();
- myUser.ShowDialog();
- }
3、将ContextMenu与寄托的母体产生关联
本将是将ContextMenu关联到grid的右键事件中弹出。
- private void gridAll_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
- {
- ContextMenu cm = this.FindResource("rightKeyMenu") as ContextMenu;
- cm.PlacementTarget = sender as Grid;
- cm.IsOpen = true;
- }
参考文献:
http://www.c-sharpcorner.com/uploadfile/mahesh/menus-in-wpf/
http://wpftutorial.net/ContextMenu.html
http://www.wpf-tutorial.com/common-interface-controls/contextmenu/