ContextMenu的使用

简介:

ContextMenu的使用

下面代码的效果是右键单击图片时,显示菜单。当单击菜单的某项时,执行相应的命令。

Image.RightTapped += new  RightTappedEventHandler(Image_RightTapped);

 

async void  Image_RightTapped( object  sender, RightTappedRoutedEventArgs e)
{
     var  menu = new  PopupMenu();
     menu.Commands.Add( new  UICommand( "Open with" , (command) =>
         {
             Display.Text = "'"  + command.Label + "' selected" ;
         }));
     menu.Commands.Add( new  UICommand( "Save attachment" , (command) =>
     {
         Display.Text = "'"  + command.Label + "' selected" ;
     }));
 
     var  chosenCommand = await menu.ShowForSelectionAsync(GetElementRect((FrameworkElement)sender));
     if  (chosenCommand == null )
     {
         Display.Text = "Context menu dismissed" ;
     }
}

  

Rect GetElementRect(FrameworkElement element)
{
     GeneralTransform buttonTransform = element.TransformToVisual( null );
     Point point = buttonTransform.TransformPoint( new  Point());
     return  new  Rect(point, new  Size(element.ActualWidth, element.ActualHeight));
}

  效果图:

 

2、文本的ContextMenuOpening事件

Scenario2TextBox.ContextMenuOpening += new ContextMenuOpeningEventHandler(Scenario2TextBox_ContextMenuOpening);

async void  Scenario2TextBox_ContextMenuOpening( object  sender, ContextMenuEventArgs e)
{
     // Create a menu and add commands specifying an id value for each instead of a delegate.
     var  menu = new  PopupMenu();
     menu.Commands.Add( new  UICommand( "Copy" , null , 1));
     menu.Commands.Add( new  UICommandSeparator());
     menu.Commands.Add( new  UICommand( "Highlight" , null , 2));
     menu.Commands.Add( new  UICommand( "Look up on dictionary" , null , 3));
 
     // We don't want to obscure content, so pass in a rectangle representing the sender of the context menu event.
     // We registered command callbacks; no need to await and handle context menu completion
     var  chosenCommand = await menu.ShowForSelectionAsync(GetElementRect((FrameworkElement)sender));
     if  (chosenCommand != null )
     {
         switch  (( int )chosenCommand.Id)
         {
             case  1:
                 Output2Text.Text = "'"  + chosenCommand.Label + "'("  + chosenCommand.Id.ToString() + ") selected" ;
                 break ;
 
             case  2:
                 Output2Text.Text = "'"  + chosenCommand.Label + "'("  + chosenCommand.Id.ToString() + ") selected" ;
                 break ;
 
             case  3:
                 Output2Text.Text = "'"  + chosenCommand.Label + "'("  + chosenCommand.Id.ToString() + ") selected" ;
                 break ;
         }
     }
     else
     {
         Output2Text.Text = "Context menu dismissed" ;
     }
}

  效果图:

效果是右键文本框时,弹出菜单。如上图。

 


本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2012/03/27/2403972.html,如需转载请自行联系原作者

目录
相关文章
|
6天前
|
Python
Combobox
在Tkinter中,下拉菜单(Combobox)是一种常用的组件,它允许用户从一组预定义的选项中选择一个或多个值。下面是关于如何在Tkinter中使用下拉菜单组件的详细说明:
29 1
|
6月前
|
JavaScript 小程序
picker bindchange="bindPickerChange" 点击事件
picker bindchange="bindPickerChange" 点击事件
79 1
|
11月前
|
存储 C++ 开发者
QListWidget和QListView的使用和item点击事件
QListWidget和QListView的使用和item点击事件
ListView onItemLongClick 弹出ContextMenu
ListView onItemLongClick 弹出ContextMenu
92 0
双击事件与单击事件的那些事
双击事件与单击事件的那些事
469 0
解决popup不随着window一起移动的问题
原文:解决popup不随着window一起移动的问题 当我们设置Popup的StayOpen="True"时,会发现移动窗体或者改变窗体的Size的时候,Popup并不会跟随着一起移动位置。为了解决这个问题,可以给Popup定义一个附加属性,代码如下所示: /// /// Popup帮助类...
1050 0
WPF listview item mouse enter/over popup
This is because the routing strategy of the Loaded event is Direct, which means that the routed event does not route though an element tree.
959 0