Silverlight4就可以创建右键菜单,但是需要多加几个方法限制SL自带的右键菜单的显示。
在SL5里面创建右键菜单就相对简单一些,这里是写的一个通用方法的简单例子。
程序集 System.Windows.Controls.Input.Toolkit.dll
using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace SinoChemBI.Tools { public class RightMenuTools { #region 创建截图右键菜单菜单 public event EventHandler OnPrintScreenMenuItemClick; ///<summary> /// 创建截图右键菜单菜单 ///</summary> public void CreatePrintScreenRightMenu(UIElement _UIElement) { ContextMenu newMenu = new ContextMenu(); MenuItem _PrintScreen = new MenuItem(); _PrintScreen.Header = "另存为图片"; _PrintScreen.Click += (s,e) => { if (OnPrintScreenMenuItemClick != null) { OnPrintScreenMenuItemClick(_UIElement,e); } }; newMenu.Items.Add(_PrintScreen); ContextMenuService.SetContextMenu(_UIElement,newMenu);//为控件绑定右键菜单 } #endregion } }
使用方法:
/// <summary> /// 初始化加载 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void WorkloadStatistics_Loaded(object sender,RoutedEventArgs e) { //绑定右键菜单 RightMenuTools _RightMenuTools1 = new RightMenuTools(); _RightMenuTools1.OnPrintScreenMenuItemClick += new EventHandler(_RightMenuTools1_OnPrintScreenMenuItemClick); _RightMenuTools1.CreatePrintScreenRightMenu(Paretochart1); //LoadData(); } void _RightMenuTools1_OnPrintScreenMenuItemClick(object sender,EventArgs e) { bool? _IsSuccess = _Imagetools.PrintScreen(this.Paretochart1);//这里调用的是我上一篇博客中的截图工具类 if (_IsSuccess == true) { MessageBox.Show("保存成功"); } else if (_IsSuccess == false) { MessageBox.Show("保存失败"); } }