/// <summary> /// 在父菜单下添加一个菜单项 /// </summary> /// <typeparam name="T">ContextMenu或者MenuItem</typeparam> /// <param name="commandItem">菜单项</param> /// <param name="parentMenu">父菜单</param> /// <param name="action">菜单消息处理过程</param> /// <param name="language">使用的语言</param> /// <returns></returns> public static MenuItem AddOneItem <T>(ChartMenuItems commandItem, T parentMenu, System.Windows.RoutedEventHandler action, Common.EnumLanguage language) where T : class { MenuItem curmenu = new MenuItem(); curmenu.Name = commandItem.ToString(); curmenu.Header = Common.Extenstion.EnumExtensions.GetEnumDescription(commandItem, language); curmenu.Click += action; if (parentMenu is ContextMenu) { (parentMenu as ContextMenu).Items.Add(curmenu); } else if (parentMenu is MenuItem) { (parentMenu as MenuItem).Items.Add(curmenu); } return(curmenu); }
/// <summary> /// 绑定弹出菜单和操作工具条 /// </summary> /// <param name="contextMenu">弹出菜单</param> /// <param name="panel">图形操作按钮面板</param> public static Dictionary <ChartMenuItems, System.Windows.Controls.Primitives.ButtonBase> BindMenuWithOperatePanel(ContextMenu contextMenu, GraphicOperatePanel panel) { if (panel == null) { return(new Dictionary <ChartMenuItems, System.Windows.Controls.Primitives.ButtonBase>()); } //创建操作按钮和菜单的对应关系 var operateButtons = new Dictionary <ChartMenuItems, System.Windows.Controls.Primitives.ButtonBase>(); var allbuttons = panel.GetAllButtons(); foreach (var btn in allbuttons) { ChartMenuItems menuItem = ChartMenuItems.Display; if (btn == panel.btnSelect) { menuItem = ChartMenuItems.Select; } else if (btn == panel.btnMove) { menuItem = ChartMenuItems.Pan; } else if (btn == panel.btnZoomIn) { menuItem = ChartMenuItems.zoomIn; } else if (btn == panel.btnZoomOut) { menuItem = ChartMenuItems.zoomOut; } else if (btn == panel.btnInformation) { menuItem = ChartMenuItems.showInformation; } else if (btn == panel.btnUpPeakPick) { menuItem = ChartMenuItems.upPeakPick; } else if (btn == panel.btnDownPeakPick) { menuItem = ChartMenuItems.downPeakPick; } else if (btn == panel.btnSizeAll) { menuItem = ChartMenuItems.resetXY; } else if (btn == panel.btnSizeYAxis) { menuItem = ChartMenuItems.resetY; } else if (btn == panel.btnColor) { menuItem = ChartMenuItems.Colors; } else if (btn == panel.btnHide) { menuItem = ChartMenuItems.Hide; } else if (btn == panel.btnGridShow) { menuItem = ChartMenuItems.showGridLine; } else { continue; } operateButtons.Add(menuItem, btn); } //按钮和菜单间的属性绑定 foreach (var item in operateButtons) { MenuItem menu = FindChildByName(contextMenu, item.Key.ToString()) as MenuItem; if (menu != null) { //刷新按钮属性 item.Value.IsEnabled = menu.IsEnabled; item.Value.ToolTip = menu.Header; item.Value.Visibility = menu.Visibility; item.Value.ToolTip = menu.Header; if (item.Value is RadioButton) { (item.Value as RadioButton).IsChecked = menu.IsChecked; Binding checkbind = new Binding("IsChecked"); checkbind.Mode = BindingMode.TwoWay; checkbind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; menu.IsCheckable = true; menu.SetBinding(MenuItem.IsCheckedProperty, checkbind); } Binding bind = new Binding("IsEnabled"); bind.Mode = BindingMode.TwoWay; bind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; menu.SetBinding(MenuItem.IsEnabledProperty, bind); bind = new Binding("Visibility"); bind.Mode = BindingMode.TwoWay; bind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; menu.SetBinding(MenuItem.VisibilityProperty, bind); bind = new Binding("ToolTip"); bind.Mode = BindingMode.TwoWay; bind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; menu.SetBinding(MenuItem.ToolTipProperty, bind); menu.DataContext = item.Value; } } return(operateButtons); }