获取上下文菜单的控制

编程入门 行业动态 更新时间:2024-10-28 00:21:54
本文介绍了获取上下文菜单的控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个看起来像这样的上下文菜单

I have a context menu that looks like this

A |--1 |--2 |--3

我需要访问该上下文菜单从调用的对象,选择1 2或3之后

I need to access the object that the context menu is called from, after selecting 1 2 or 3

的意思,如果这是一个TextBox1中的上下文菜单,然后我需要访问该对象,我该怎么办呢?

meaning if this is a context menu of a textbox1 then I need to access that object, how do I do that?

忘了提,这是一个WPF应用程序。因此,使用IM的System.Windows.Controls的 而文本菜单的编程方式创建

Forgot to mention, this is a WPF application. so Im using the System.Windows.Controls and the ContextMenu is created programmatically

推荐答案

您可以走了树,并从 ContextMenu.PlacementTarget ,如:

You can walk up the tree and get the control from the ContextMenu.PlacementTarget, e.g.

private void MenuItem_Click(object sender, RoutedEventArgs e) { var item = sender as MenuItem; while (item.Parent is MenuItem) { item = (MenuItem)item.Parent; } var menu = item.Parent as ContextMenu; if (menu != null) { var droidsYouAreLookingFor = menu.PlacementTarget as TextBox; //... } }

更多推荐

获取上下文菜单的控制

本文发布于:2023-11-23 02:23:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619739.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上下文   菜单

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!