WP7 查找 ContextMenu 的所有者
我通过读取 XML 文件动态填充应用程序中 StackPanel
的内容。 StackPanel
的每个子级本身就是另一个 StackPanel
,其中包含不同的 UIElement
对象。嵌套 StackPanel
设计的原因是因为我想将 3 个不同 ContextMenu
之一与这些子 StackPanel
关联。
因此结构如下所示:
---- StackPanel parent
|
---- StackPanel child
| |
| ---- TextBlock
|
---- StackPanel child
| |
| ---- TextBox
|
---- StackPanel child
| |
| ---- Image
|
.
.
.
对于每个 StackPanel
子项,我从 3 个 ContextMenu
中进行选择,并按如下方式附加它们:
var stackPanels = parentStackPanel.Children.OfType<StackPanel>();
for( int i = 0; i < stackPanels.Count(); ++i ) {
if( someCondition ) {
ContextMenuService.SetContextMenu( stackPanels.ElementAt( i ), MyContextMenu1 );
} else if( someOtherCondition ) {
ContextMenuService.SetContextMenu( stackPanels.ElementAt( i ), MyContextMenu2 );
} else {
ContextMenuService.SetContextMenu( stackPanels.ElementAt( i ), MyContextMenu3 );
}
}
所有 MenuItem
所有 3 个 ContextMenu 都有相同的 Click 处理程序。
现在,最后一个问题是:如何确定哪个 StackPanel
子项的 ContextMenu
被调用并单击?检查调试器中单击处理程序中的发送者对象显示,ContextMenu
有一个名为 Owner
的内部
DependencyObject
,它包含对 StackPanel 的引用,这正是我想要的,但当然,我无法以这种方式在代码中访问它。
我可以通过向每个子 StackPanel
添加一个 MouseLeftButtonDown
处理程序来解决该问题,保存最后选择的一个,然后在 ContextMenu
中检索它处理程序,但这个解决方案感觉有点难看。有更好的方法吗?
预先感谢您的帮助!
I am dynamically populating the contents of a StackPanel
in my application by reading an XML file. Each child of the StackPanel
is itself another StackPanel
which contains different UIElement
objects. The reason for the nested StackPanel
design is because I want to associate one of 3 different ContextMenu
s with of these child StackPanel
s.
So the structure looks like this:
---- StackPanel parent
|
---- StackPanel child
| |
| ---- TextBlock
|
---- StackPanel child
| |
| ---- TextBox
|
---- StackPanel child
| |
| ---- Image
|
.
.
.
For each StackPanel
child I'm choosing from amongst 3 ContextMenu
s and attaching them as follows:
var stackPanels = parentStackPanel.Children.OfType<StackPanel>();
for( int i = 0; i < stackPanels.Count(); ++i ) {
if( someCondition ) {
ContextMenuService.SetContextMenu( stackPanels.ElementAt( i ), MyContextMenu1 );
} else if( someOtherCondition ) {
ContextMenuService.SetContextMenu( stackPanels.ElementAt( i ), MyContextMenu2 );
} else {
ContextMenuService.SetContextMenu( stackPanels.ElementAt( i ), MyContextMenu3 );
}
}
All MenuItem
s under all 3 ContextMenu
s have the same Click
handler.
Now, finally, the question: How do I determine which StackPanel
child's ContextMenu
was invoked and clicked? Inspecting the sender object within the click handler in the debugger shows that a ContextMenu
has an internal
DependencyObject
named Owner
which contains a reference to the StackPanel
, this is exactly what I want but of course, I can't access it in code that way.
I could solve the problem by adding a MouseLeftButtonDown
handler to each child StackPanel
, saving the one that was last selected and then retrieving this within the ContextMenu
handler but this solution feels a little ugly. Is there a better way to do this?
Thanks in advance for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在点击事件处理程序中将
sender
转换为UIElement
,您应该能够获得识别被点击的实际项目所需的任何信息。或者转换为 DependencyObject(如果可能)并使用它来遍历可视化树:
If you cast the
sender
as aUIElement
in the click event handler you should be able to get at whatever you need to identify the actual item which was clicked.Alternatively cast as a DependencyObject (if possible) and use that to walk the visual Tree: