WP7 查找 ContextMenu 的所有者

发布于 2024-10-09 20:04:28 字数 1672 浏览 0 评论 0原文

我通过读取 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 ContextMenus with of these child StackPanels.

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 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 );

  }
}

All MenuItems under all 3 ContextMenus 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

池木 2024-10-16 20:04:28

如果您在点击事件处理程序中将 sender 转换为 UIElement,您应该能够获得识别被点击的实际项目所需的任何信息。

(sender as UIElement).Property

或者转换为 DependencyObject(如果可能)并使用它来遍历可视化树:

VisualTreeHelper.GetParent((sender as DependencyObject))

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

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

VisualTreeHelper.GetParent((sender as DependencyObject))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文