获取上下文菜单的控制上下文、菜单

由网友(血腥玛丽)分享简介:我有一个看起来像这样的上下文菜单I have a context menu that looks like thisA|--1|--2|--3我需要访问该上下文菜单从调用的对象,选择1 2或3之后I need to access the object that the context menu is called...

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

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;
        //...
    }
}
阅读全文

相关推荐

最新文章