[英]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.