WP7 - 取消ContextMenu单击事件传播 [英] WP7 - Cancelling ContextMenu click event propagation
问题描述
当Silverlight工具包的 ContextMenu
在 UIElement
之间被点击时,我遇到问题,注册了一个Tap事件 GestureListener
。上下文菜单单击传播到底层元素,并触发其点击事件。
I'm having a problem when the Silverlight toolkit's ContextMenu
is clicked while it is over a UIElement
that has registered a Tap event GestureListener
. The context menu click propagates to the underlying element and fires its tap event.
例如,说我有一个 ListBox
和每个 ListBoxItem
其中注册了一个 ContextMenu
和一个Tap GestureListener
。假设单击上下文菜单项2应该会带你到Page1.xaml,而点击任何ListBox项目本身应该带你到Page2.xaml。
For instance, say I have a ListBox
and each ListBoxItem
within it has registered both a ContextMenu
and a Tap GestureListener
. Assume that clicking context menu item2 is supposed to take you to Page1.xaml, while tapping on any of ListBox items themselves is supposed to take you to Page2.xaml.
如果我打开ListBox中item1的上下文菜单,然后上下文菜单项2位于ListBox item2的顶部。当我点击上下文菜单项2时,我感到怪异的行为,应用程序导航到Page1.xaml,然后立即到Page2.xaml,因为点击事件也触发了点击手势 ListBox
item2。
If I open the context menu on item1 in the ListBox, then context menu item2 is on top of ListBox item2. When I click on context menu item2 I get weird behavior where the app navigates to Page1.xaml and then immediately to Page2.xaml because the click event also triggered the Tap gesture for ListBox
item2.
我已经在调试器中验证了它始终是接收点击事件的上下文菜单。如何取消上下文菜单项点击的路由事件传播,因此它不会到达ListBox item2?
I've verified in the debugger that it is always the context menu that receives the click event first. How do I cancel the context menu item click's routed event propagation so it doesn't reach ListBox item2?
感谢您的帮助!
推荐答案
您可以通过执行以下操作来解决问题:
You can get around the problem by doing the following:
-
在上下文菜单的
已打开
处理程序集LayoutRoot.IsHitTestVisible
(LayoutRoot
是根UIElement
)到false
In the context menu's
Opened
handler setLayoutRoot.IsHitTestVisible
(LayoutRoot
is the default name for the rootUIElement
) tofalse
在上下文菜单的 Closed
处理程序集 LayoutRoot.IsHitTestVisible
返回到 true
In the context menu's Closed
handler set LayoutRoot.IsHitTestVisible
back to true
这篇关于WP7 - 取消ContextMenu单击事件传播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!