“右键”菜单是怎样添加进去的,为什么只在“矩形区域”有“右键菜单”???
[img=http://social.msdn.microsoft.com/Forums/getfile/142020][/img]
对应的XAML如下:
<UserControl x:Class="SLRightMouseButton.MainPage"
xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="640" Height="480">
<Canvas x:Name="LayoutRoot">
<Rectangle x:Name="ti" Width="150" Height="150" RadiusX="5" RadiusY="5" Fill="Blue"></Rectangle>
<my:ContextMenuService.ContextMenu>
<my:ContextMenu Name="mymenu">
<my:MenuItem Header="关联地区" Click="MenuItem_Click"/>
<my:Separator/>
<my:MenuItem Header="关联页面" Click="MenuItem_Click"/>
<my:Separator/>
<my:MenuItem Header="进入全屏" Click="MenuItem_Click"/>
</my:ContextMenu>
</my:ContextMenuService.ContextMenu>
</Canvas>
</UserControl>
后台代码:
namespace SLRightMouseButton
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
/// <summary>
/// 鼠标右键选择相应的项的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
MenuItem menuItem = (MenuItem)sender;
switch (menuItem.Header.ToString())
{
case "关联地区":
MessageBox.Show("关联地区");//在此可以弹出一个窗口显示关联地区的子窗口。然后选择相应的数据,为此Rectangle关联地区