参考了许多文章,觉得写得挺好,但是总是不能简化说明。
比如右键菜单,我们最关心的是如何屏蔽掉默认的菜单,
如何在右击某个控件的时候显示对应的右键菜单。
如何构造生成右键菜单,如何响应右键菜单的事件。
这些问题寥寥几句代码就能说明问题了,但是高手都没有直说。
当然这些文章包含了许多其他的知识点,值得以后参考。
以下是我的简单实现:
大气象
大气象
比如右键菜单,我们最关心的是如何屏蔽掉默认的菜单,
如何在右击某个控件的时候显示对应的右键菜单。
如何构造生成右键菜单,如何响应右键菜单的事件。
这些问题寥寥几句代码就能说明问题了,但是高手都没有直说。
当然这些文章包含了许多其他的知识点,值得以后参考。
如图:
以下是我的简单实现:


<
UserControl
x:Class
="TestSilverlightRightMouseClickDemo.UC_RightButton"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable ="d"
d:DesignHeight ="300" d:DesignWidth ="400" >
< Grid x:Name ="LayoutRoot" Background ="White" MouseRightButtonDown ="LayoutRoot_MouseRightButtonDown" >
< Button Content ="右键菜单" Height ="23" HorizontalAlignment ="Left" Margin ="10,10,0,0" Name ="btnRight" VerticalAlignment ="Top" Width ="75" />
</ Grid >
</ UserControl >
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable ="d"
d:DesignHeight ="300" d:DesignWidth ="400" >
< Grid x:Name ="LayoutRoot" Background ="White" MouseRightButtonDown ="LayoutRoot_MouseRightButtonDown" >
< Button Content ="右键菜单" Height ="23" HorizontalAlignment ="Left" Margin ="10,10,0,0" Name ="btnRight" VerticalAlignment ="Top" Width ="75" />
</ Grid >
</ UserControl >


using
System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace TestSilverlightRightMouseClickDemo
{
public partial class UC_RightButton : UserControl
{
public UC_RightButton()
{
InitializeComponent();
BindMenu(); // 只为按钮控件绑定右键菜单
}
private void BindMenu()
{
ContextMenu cm = new ContextMenu(); // 新建右键菜单
MenuItem mi = new MenuItem(); // 新建右键菜单项
mi.Header = " 菜单项 " ;
mi.Click += MenuItem_Click; // 为菜单项注册事件
cm.Items.Add(mi);
ContextMenuService.SetContextMenu(btnRight, cm); // 为控件绑定右键菜单
}
private void LayoutRoot_MouseRightButtonDown( object sender, MouseButtonEventArgs e)
{
e.Handled = true ; // 屏蔽默认的右键菜单
}
private void MenuItem_Click( object sender, RoutedEventArgs e)
{
MessageBox.Show( " 右键菜单事件 " );
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace TestSilverlightRightMouseClickDemo
{
public partial class UC_RightButton : UserControl
{
public UC_RightButton()
{
InitializeComponent();
BindMenu(); // 只为按钮控件绑定右键菜单
}
private void BindMenu()
{
ContextMenu cm = new ContextMenu(); // 新建右键菜单
MenuItem mi = new MenuItem(); // 新建右键菜单项
mi.Header = " 菜单项 " ;
mi.Click += MenuItem_Click; // 为菜单项注册事件
cm.Items.Add(mi);
ContextMenuService.SetContextMenu(btnRight, cm); // 为控件绑定右键菜单
}
private void LayoutRoot_MouseRightButtonDown( object sender, MouseButtonEventArgs e)
{
e.Handled = true ; // 屏蔽默认的右键菜单
}
private void MenuItem_Click( object sender, RoutedEventArgs e)
{
MessageBox.Show( " 右键菜单事件 " );
}
}
}
参考:
风云的银光志Silverlight4.0教程之使用鼠标右键事件和滚轮事件
Silverlight4给DataGrid添加ContextMenu右键菜单