获取上下文菜单的控制 [英] Getting the control of a context menu

查看:170
本文介绍了获取上下文菜单的控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

您可以走了树,并从<一个控制href="http://msdn.microsoft.com/en-us/library/system.windows.controls.contextmenu.placementtarget.aspx"相对=nofollow> 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;
        //...
    }
}

这篇关于获取上下文菜单的控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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