分别通过后台和前台两种方式说明在Silverlight中右键菜单的使用
首先要安装Silverlight Toolkit,在项目中添加对System.windows.Controls.input.Toolkit 程序集的引用
1、直接在XAML中定义右键菜单,在XAML中添加命名空间xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit",下面代码是为StackPanel添加右键菜单
<StackPanel OrIEntation="Horizontal" > <TextBlock Text="1234" margin="5,0"/> <toolkit:ContextMenuService.ContextMenu> <toolkit:ContextMenu> <toolkit:ContextMenu.Items> <toolkit:MenuItem header="启用" /> <toolkit:MenuItem header="禁用" /> </toolkit:ContextMenu.Items> </toolkit:ContextMenu> </toolkit:ContextMenuService.ContextMenu></StackPanel>
2、通过后台代码添加右键菜单,如下
voID Loaded(object sender,RoutedEventArgs e) { ContextMenu contextMenu = new ContextMenu(); contextMenu.Background = new SolIDcolorBrush(colors.transparent); MenuItem item1 = new MenuItem(); item1.header = "红色"; item1.Click += new RoutedEventHandler(item_Click); contextMenu.Items.Add(item1); MenuItem item2 = new MenuItem(); item2.header = "蓝色"; item2.Click += new RoutedEventHandler(item_Click); contextMenu.Items.Add(item2); MenuItem item3 = new MenuItem(); item3.header = "绿色"; item3.Click += new RoutedEventHandler(item_Click); contextMenu.Items.Add(item3); ContextMenuService.SetContextMenu(txtname,contextMenu); } voID item_Click(object sender,RoutedEventArgs e) { MenuItem item = sender as MenuItem; //ContextMenu parent = item.Parent as ContextMenu; switch (item.header.ToString()) { case "红色": ellipse1.Fill = new SolIDcolorBrush(colors.Red); break; case "蓝色": ellipse1.Fill = new SolIDcolorBrush(colors.Blue); break; case "绿色": ellipse1.Fill = new SolIDcolorBrush(colors.Green); break; default: break; } }总结
以上是内存溢出为你收集整理的Silverlight ContextMenu用法全部内容,希望文章能够帮你解决Silverlight ContextMenu用法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)