本文介绍了获取上下文菜单的控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个看起来像这样的上下文菜单
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; //... } }更多推荐
获取上下文菜单的控制
发布评论