Silverlight右键菜单管理


在silverlight全屏的时候,键盘事件就不响应了,这时候只能用右键菜单来完成一定的操作了。

在网上看到CSDN的jianyi7659的博客中介绍了三种方法:

利用sliverlight4.0以上版本提供的MouseRightButtonDown、MouseRightButtonUp事件

注意在silerlight4.0以下的版本中,不支持鼠标右键事件,详情可看控件基类UIElement中没有

右键事件,只有左键事件,而在Silerlight4.0以上的版本中可以看到支持了右键事件

//
// 摘要:
// 当鼠标(或触笔)进入 System.Windows.UIElement 的边界区域时发生。
public event MouseEventHandler MouseEnter;
//
// 摘要:
// 当鼠标(或触笔)离开 System.Windows.UIElement 的边界区域时发生。
public event MouseEventHandler MouseLeave;
//
// 摘要:
// 当按下鼠标左键(或触笔的笔尖接触 Tablet)并且鼠标指针悬停在 System.Windows.UIElement 上时发生。
public event MouseButtonEventHandler MouseLeftButtonDown;
//
// 摘要:
// 当鼠标(或触笔)悬停在 System.Windows.UIElement 上(或 System.Windows.UIElement 具有鼠标捕获)并且用户松开鼠标左键(或从
// Tablet 上移开触笔的笔尖)时发生。
public event MouseButtonEventHandler MouseLeftButtonUp;
//
// 摘要:
// 当鼠标(或触笔)的坐标位置更改并且悬停在 System.Windows.UIElement 上(或 System.Windows.UIElement
// 具有鼠标捕获)时发生。
public event MouseEventHandler MouseMove;
//
// 摘要:
// 当鼠标指针位于 System.Windows.UIElement 上并按下鼠标右键时发生。
public event MouseButtonEventHandler MouseRightButtonDown;
//
// 摘要:
// 当鼠标指针位于 System.Windows.UIElement 上并释放鼠标右键时发生。但是,只有在调用方将前面的 System.Windows.UIElement.MouseRightButtonDown
// 事件标记为“已处理”时才引发此事件;请参见“备注”。
public event MouseButtonEventHandler MouseRightButtonUp;
//
// 摘要:
// 在以下情况下发生:当鼠标指针悬停在 System.Windows.UIElement 上时或 System.Windows.UIElement 具有焦点时,用户滚动鼠标滚轮。
public event MouseWheelEventHandler MouseWheel;

唉,不过在VS2008中后台代码中智能提示却可以打出右键事件,但编译的时候却不能通过。

只好升级成silerlight5.0,但在VS2008中不支持创建的时候选择Silerlight版本号,而且在项目中也不能更改其版号

重要的是VS2008中silerlight不能用了,没有智能提示了,没办法只能用Vs2010了。还是2010爽,呵呵

第一种,自己绘制右键菜单

自己绘制右键菜单需要写一个处理菜单的基类。该基类处理类主要负责右键菜单的弹出、定位、关闭等等

public abstract class ContextMenu
{
private Point _location;
private bool _isShowing;
private Popup _popup;
private Grid _grid;
private Canvas _canvas;
private FrameworkElement _content;

public void Show(Point location)
{
if (_isShowing)
throw new InvalidOperationException();
_isShowing = true;
_location = location;
ConstunctPopup();
_popup.IsOpen = true;
}

public void Close()
{
_isShowing = false;
if (_popup != null)
_popup.IsOpen = false;
}

protected abstract FrameworkElement GetContent();

protected virtual void OnClickOutside()
{
Close();
}

private void ConstunctPopup()
{
if (_popup != null)
return;
_popup = new Popup();
_grid = new Grid();
_popup.Child = _grid;
_canvas = new Canvas();
_canvas.MouseLeftButtonDown += (sender, args) => { OnClickOutside(); };
_canvas.MouseRightButtonDown += (sender, args) => { args.Handled = true; OnClickOutside(); };
_canvas.Background = new SolidColorBrush(Colors.Transparent);
_grid.Children.Add(_canvas);
_content = GetContent();
_content.HorizontalAlignment = HorizontalAlignment.Left;
_content.VerticalAlignment = VerticalAlignment.Top;
_content.Margin = new Thickness(_location.X, _location.Y, 0, 0);
_grid.Children.Add(_content);
UpdateSize();
}

private void UpdateSize()
{
_grid.Width = Application.Current.Host.Content.ActualWidth;
_grid.Height = Application.Current.Host.Content.ActualHeight;

if (_canvas != null)
{
_canvas.Width = _grid.Width;
_canvas.Height = _grid.Height;
}
}
}

Ⅱ右键菜单界面的实现

上面我们已经构建了一个右键菜单的处理基类,现在就让我们来写一个右键菜单的界面吧!

public class WFContextMenu : ContextMenu
{
WorkFlow.Element.Nodes _nodes;

public WFContextMenu(WorkFlow.Element.Nodes node)
{
_nodes = node;
}

protected override FrameworkElement GetContent()
{
Border border = new Border() { BorderBrush = new SolidColorBrush(Color.FromArgb(255, 167, 171, 176)), BorderThickness = new Thickness(1), Background = new SolidColorBrush(Colors.White) };
border.Effect = new DropShadowEffect() { BlurRadius = 3, Color = Color.FromArgb(255, 230, 227, 236) };
border.MouseRightButtonDown += (sender, args) => { args.Handled = true; };

Grid grid = new Grid() { Margin = new Thickness(1) };
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(80) });

//modify
Button modifyButton = new Button() { Height = 22, Margin = new Thickness(0, 0, 0, 0) ,VerticalAlignment=VerticalAlignment.Top};
modifyButton.Click += modify_MouseLeftButtonUp;
modifyButton.Content = "修改";
grid.Children.Add(modifyButton);

//delete
Button deleteButton = new Button() { Height = 22, Margin = new Thickness(0, 24, 0, 0), VerticalAlignment = VerticalAlignment.Top };
deleteButton.Click += delete_MouseLeftButtonUp;
deleteButton.Content = "删除";
grid.Children.Add(deleteButton);

border.Child = grid;
return border;
}

void modify_MouseLeftButtonUp(object sender, RoutedEventArgs e)
{
WorkFlow.Control.NodesSet.ShowObject(_nodes);
Close();
}

void delete_MouseLeftButtonUp(object sender, RoutedEventArgs e)
{
if (System.Windows.Browser.HtmlPage.Window.Confirm("确定删除选择的节点吗?"))
{
int s = _nodes.Container.NodesList.Count;
for (int i = 0; i < s; i++)
{
if (_nodes.Container.NodesList[i].IsSelect)
{
_nodes.Container.RemoveNodes(_nodes.Container.NodesList[i]);
s--;
i = -1;
}
}
Close();
}
}
}


优质内容筛选与推荐>>
1、2 app应用广告推广
2、T-SQL查询进阶--理解SQL Server中索引的概念,原理以及其他(转)
3、Ubuntu8.10安装Netbeans6.7中文乱码解决方案
4、Spring MVC配置
5、Docker存储驱动之Device Mapper简介


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn