简体   繁体   English

选择项目后,如何防止WP7上下文菜单关闭?

[英]How can I prevent a WP7 context menu from closing after an item has been selected?

I have hooked up a context menu item in a listbox item such that tapping it will change its state. 我已经将上下文菜单项连接到列表框项中,因此点击它会更改其状态。 I need either the menu to stay open after the item has been selected, or to programmatically reopen the menu right after it closes. 我需要菜单在选定项目后保持打开状态,或者需要在菜单关闭后以编程方式重新打开菜单。

My menu looks like so: 我的菜单如下所示:

Some Command 1
Some Command 2
Some Command 3
Inverted

And the user can tap the Inverted command and then tap one of the other commands to cause them to function in Inverted Mode, and the menu through data binding appears like so: 用户可以点击“ Inverted命令,然后点击其他命令之一,使它们在“反转模式”下运行,通过数据绑定的菜单如下所示:

Some Command 1
Some Command 2
Some Command 3
Inverted ✔ 

Not being able to figure out how to keep the menu open after tap, I've tried the less ideal reopen menu approach like so: 无法弄清楚如何在点击后保持菜单打开,我尝试了不太理想的重新打开菜单方法,如下所示:

private void onCommandInvert(object sender, RoutedEventArgs e)
{
  CommandState.Instance.Inverted = !CommandState.Instance.Inverted;

  // Open it again.
  MenuItem menuItem = (MenuItem)sender;
  ContextMenu menu = (ContextMenu)menuItem.Parent;
  menu.IsOpen = true;
}

But doing so throws the following exception on the menu.IsOpen = true statement: 但是这样做会在menu.IsOpen = true上引发以下异常menu.IsOpen = true语句:

A first chance exception of type 'System.InvalidOperationException' occurred in 
  System.Windows.dll

An unhandled exception of type 'System.InvalidOperationException' occurred in 
  System.Windows.dll

Additional information: Element is already the child of another element.

I have also tried the following with the Closed event, with the same exception occurring: 我还尝试了Closed事件进行以下操作,但发生了相同的异常:

private void onContextMenuClosed(object sender, RoutedEventArgs e)
{
  ContextMenu menu = (ContextMenu)sender;
  menu.IsOpen = true;
}

Any ideas? 有任何想法吗? Thanks! 谢谢!

I got it! 我知道了! Thanks to willmel's comment, I digged through the source code for the MenuItem and was able to override OnClick() to do exactly what I needed (the ideal solution no doubt). 感谢willmel的评论,我浏览了MenuItem的源代码,并且能够重写OnClick()来精确地执行我需要的操作(毫无疑问是理想的解决方案)。 I couldn't access Click however, so I needed to introduce a StayClick event property as well. 但是,我无法访问Click ,因此还需要引入StayClick事件属性。

Enjoy! 请享用!

using Microsoft.Phone.Controls;
using System.Windows;

namespace MyNamespace
{
  public class MenuItemEx : MenuItem
  {
    public bool StayOpenWhenClicked
    {
      get;
      set;
    }

    public event RoutedEventHandler StayClick;

    protected override void OnClick()
    {
      if (StayOpenWhenClicked)
      {
        if (StayClick != null)
        {
          StayClick.Invoke(this, new RoutedEventArgs());
        }
      }
      else
      {
        base.OnClick();
      }
    }
  }
}

and in the page's xaml, instead of toolkit:MenuItem you use my:MenuItemEx 并在页面的xaml中,使用my:MenuItemEx而不是toolkit:MenuItem my:MenuItemEx

<my:MenuItemEx 
  Header="Inverted"             
  StayClick="onCommandInvert"
  StayOpenWhenClicked="True"
/>

If you want to persist the menu after the user has selected an item then I believe the Context menu control is not what you should be using. 如果您要在用户选择一个项目后保留菜单,那么我相信上下文菜单控件不是您应该使用的菜单控件。

Better you should create your own user control to mimic the behaviour and have it place appropriately on the screen where it makes sense (to the side or above/below) 更好的是,您应该创建自己的用户控件来模仿该行为,并将其适当地放置在有意义的屏幕上(侧面或上方/下方)

Alternatively if these are options to be enacted on selected items consider using the application bar icons / menu items and write the event code to read the currently selected value of the listbox item. 或者,如果这些是要在选定项目上执行的选项,请考虑使用应用程序栏图标/菜单项并编写事件代码以读取列表框项目的当前选定值。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在WP7中以编程方式设置选定的Panorama项目 - How to programmatically set selected Panorama item in WP7 WP7 ListPicker-从数据源生成时如何标记模型中的选定项目? - WP7 ListPicker - How to mark selected item in model when generated from DataSource? 将上下文菜单添加到wp7中的DataTemplate项目 - Adding Context menu to DataTemplate Items in wp7 禁用Listpicker WP7上的选定项目突出显示 - Disable Selected Item highlight on Listpicker WP7 一旦选择了项目,如何刷新自动完成框(即从中清除文本)? - How to refresh (i.e. clear text from) an autocompletebox once an item has been selected? 在WP7 + Silverlight中,如何更改列表框项目的视觉状态? - In WP7 + Silverlight how can I change the Visual State of an ListBox Item? 如何在wp7中更改列表框项目的可见性属性? - How can I change listbox item's visibility property in wp7? 如何从silverlight中的不可编辑文本框中为wp7获取所选文本 - How do I get selected text from a non-editable textbox in silverlight for wp7 WP7/XNA/Silverlight:如何在绘制 TriangleStrip 后绘制 spriteBatch? - WP7/XNA/Silverlight: How can I draw a spriteBatch after I draw a TriangleStrip? WP7:更改所选列表框项目中项目的可见性 - WP7 : Change the visibility of an item in a selected listbox item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM