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("保存失败"); } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。