运用Popup,自定义Silverlight右键菜单

   当我们打开silverlight的页面点击右键的时候,会千篇一律的出现那个系统菜单。有时候在项目中我们需要一些特殊的右键菜单设置。有了Popup我们就可以自己定义了。定义自己想要的右键菜单。直接上代码:

一,前台代码:

 1 <UserControl x:Class="SLTest.MainPage"
 2     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6     mc:Ignorable="d"
 7     d:DesignHeight="300" d:DesignWidth="400">
 8 
 9     <Grid x:Name="LayoutRoot" Background="Transparent">
10         <TextBlock x:Name="txtStatus"  Width="300" HorizontalAlignment="Center"/>
11         <Grid x:Name="zone" Height="200" Width="200" Background="Wheat" >
12         </Grid>
13         </Grid>
14 </UserControl>

 前台很简单,我想要的效果是在名称为zone的Grid区域内,显示自定义的菜单,屏蔽掉系统自带的右键。
二,后台代码

 1     public partial class MainPage : UserControl
 2     {
 3         Popup rootPopup = new Popup();//声明一个Popup对象
 4 
 5         public MainPage()
 6         {
 7             InitializeComponent();
 8             Loaded += new RoutedEventHandler(MainPage_Loaded);
 9         }
10 
11         void MainPage_Loaded(object sender, RoutedEventArgs e)
12         {
13             //zone 区域的鼠标左键点击事件,这两个事件必须同时注册,否则屏蔽不掉系统右键菜单
14             zone.MouseRightButtonDown += new MouseButtonEventHandler(zone_MouseRightButtonDown);
15             zone.MouseRightButtonUp += new MouseButtonEventHandler(zone_MouseRightButtonUp);
16         }
17 
18         /// <summary>
19         /// 鼠标左键释放事件
20         /// </summary>
21         /// <param name="sender"></param>
22         /// <param name="e"></param>
23         void zone_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
24         {
25             rootPopup.IsOpen = true;//显示Popup
26             rootPopup.Width = App.Current.Host.Content.ActualWidth;//设置Popup的宽度即当前区域的宽度
27             rootPopup.Height = App.Current.Host.Content.ActualHeight;//设置Popup的高度即当前区域的高度
28 
29             Grid rootGrid = new Grid();
30             ListBox listBox = new ListBox();
31 
32             for (int i = 0; i < 5; i++)
33             {
34                 TextBlock txt = new TextBlock() { Text = "Item" + i.ToString() };//声明TextBlock对象即自定义菜单的项
35                 txt.MouseLeftButtonUp += new MouseButtonEventHandler(txt_MouseLeftButtonUp);//注册TextBlock的鼠标点击事件
36                 listBox.Items.Add(txt);
37             }
38             rootGrid.Children.Add(listBox);
39 
40             Point point = e.GetPosition(LayoutRoot);//获得当前鼠标点击的坐标
41             rootPopup.Child = rootGrid;//设置Propup的承载的内容为rootGrid
42             rootPopup.Margin = new Thickness(point.X, point.Y, 0, 0);//设置Propup的显示的位置
43         }
44 
45         /// <summary>
46         /// 自定义菜单的点击事件
47         /// </summary>
48         /// <param name="sender"></param>
49         /// <param name="e"></param>
50         void txt_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
51         {
52             rootPopup.IsOpen = false;//点击之后隐藏Prpup
53             txtStatus.Text = "你点击了:" + (sender as TextBlock).Text;//显示点击的内容
54         }
55 
56         /// <summary>
57         /// 鼠标左键点击事件
58         /// </summary>
59         /// <param name="sender"></param>
60         /// <param name="e"></param>
61         void zone_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
62         {
63             e.Handled = true;//屏蔽系统右键菜单
64         }
65     }

  至此已经全部完成。
运行效果如下:

原文地址:https://www.cnblogs.com/ListenCode/p/2613713.html