WP7查找ContextMenu的所有者 [英] WP7 Finding a ContextMenu's owner

查看:66
本文介绍了WP7查找ContextMenu的所有者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过读取XML文件在应用程序中动态填充 StackPanel 的内容。 StackPanel 的每个孩子本身就是另一个 StackPanel ,其中包含不同的 UIElement 对象。嵌套 StackPanel 设计的原因是因为我要将三个不同的 ContextMenu 之一与这些子 StackPanel s。

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 ContextMenus with of these child StackPanels.

因此结构如下:

  ---- StackPanel parent
       |
       ---- StackPanel child
       |    |
       |    ---- TextBlock
       |
       ---- StackPanel child
       |    |
       |    ---- TextBox
       |
       ---- StackPanel child
       |    |
       |    ---- Image
       |
       .
       .
       .

我选择的每个 StackPanel 孩子从3个 ContextMenu s中进行附加,如下所示:

For each StackPanel child I'm choosing from amongst 3 ContextMenus 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 );

  }
}

全部所有3个 ContextMenu 下的MenuItem 具有相同的 Click 处理程序。

All MenuItems under all 3 ContextMenus have the same Click handler.

现在,最后一个问题是:如何确定哪个 StackPanel 子级的 ContextMenu 被调用并单击?在调试器的单击处理程序中检查sender对象将显示 ContextMenu 具有内部 名为 Owner 的DependencyObject 包含对 StackPanel 的引用,这正是我想要的但是当然,我不能以这种方式在代码中访问它。

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.

我可以通过为每个孩子 StackPanel添加一个 MouseLeftButtonDown 处理程序来解决该问题,保存上一次选择的内容,然后在 ContextMenu 处理程序中检索此内容,但此解决方案有点难看。有更好的方法吗?

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?

在此先感谢您的帮助!

推荐答案

如果在点击事件处理程序中将发件人转换为 UIElement ,您应该能够

If you cast the sender as a UIElement in the click event handler you should be able to get at whatever you need to identify the actual item which was clicked.

(sender as UIElement).Property

或者强制转换为DependencyObject并使用它来遍历可视树:

Alternatively cast as a DependencyObject (if possible) and use that to walk the visual Tree:

VisualTreeHelper.GetParent((sender as DependencyObject))

这篇关于WP7查找ContextMenu的所有者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆