“右键菜单”是怎样添加进去的,为什么只在“矩形区域”有“右键菜单”???
“右键”菜单是怎样添加进去的,为什么只在“矩形区域”有“右键菜单”???
对应的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关联地区
break;
case "关联页面":
MessageBox.Show("关联页面");//在此可以弹出一个窗口显示关联页面的子窗口。然后选择相应的数据,为此Rectangle关联页面
break;
case "进入全屏":
menuItem.Header = FullScreens(menuItem.Header.ToString());//调用FullScreens函数来设置当前是否全屏,以及获取下次点击鼠标右键获得的右键菜单Header字。
break;
case "取消全屏":
menuItem.Header = FullScreens(menuItem.Header.ToString());
break;
default:
break;
}
mymenu.IsOpen = false;//右键菜单关闭
}
/// <summary>
/// 是否进入全屏
/// </summary>
/// <param name="IsScreen">全屏参数</param>
/// <returns></returns>
private string FullScreens(string IsScreen)
{
Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;
if (IsScreen == "进入全屏")
{
IsScreen = "取消全屏";
}
else
{
IsScreen = "进入全屏";
}
return IsScreen;
}
}
}
问题:
“右键菜单”是怎样添加进去的,为什么只在“矩形区域”有“右键菜单”???
[解决办法]
“my:ContextMenuService.ContextMenu”这个就是右键菜单???
[解决办法]
没搞过. 目测. 是不是需要做Canvas 的鼠标右键事件,然后弹出你的自定义右键菜单呢..
[解决办法]
是的,ContextMenu就是右键菜单