繁体   English   中英

如何获取已开始事件的控件的名称?

[英]How to get the name of a control that has started an event?

我需要获取启动事件的控件的名称,特别是我为DataGrid使用了ContextMenu,这是我实际编写的代码:

private void ClearTable_Click(object sender, RoutedEventArgs e)
{
    // Try to cast the sender to a MenuItem
    ContextMenu menuItem = sender as ContextMenu;
    if (menuItem != null)
    {
        // Retrieve the ContextMenu that contains this MenuItem
        ContextMenu menu = menuItem.GetContextMenu();

        // Get the control that is displaying this context menu
        Control sourceControl = menu.SourceControl;
    }
}

XAML

 <ContextMenu x:Key="Squadre_ContextMenu">
            <MenuItem Header="Pulisci Tabella" Click="ClearTable_Click">
            </MenuItem>
            <MenuItem Header="Elimina Riga selezionata" Click="ClearRow_Click">
            </MenuItem>
        </ContextMenu>

但是ContextMenu没有方法GetContextMenu,所以这对我来说是个问题。 有解决此问题的方法或另一种解决方法?

尝试这个:

private void ClearTable_Click(object sender, RoutedEventArgs e)
{
    // Try to cast the sender to a Control
    Control ctrl = sender as Control;
    if (ctrl != null)
    {
        // Get the control name
        string name = ctrl.Name;

        // Get parent control name
        Control parent = (Control) ctrl.Parent;
        string parentName = parent.Name
    }
}

编辑:添加代码以获取父控件的名称

将发送者对象作为MenuItem然后获取TextBlock控件的ContextMenu的示例:

MenuItem mi = sender as MenuItem;

if (mi != null)
{
    ContextMenu cm = mi.CommandParameter as ContextMenu;

    if (cm != null)
    {
        TextBlock t = cm.PlacementTarget as TextBlock;
        if (t != null)
        {
            // print t.Name or whatever...
        }
    }
}

希望这可以帮助

我用datagridview制作了一个表格,我的名字没有问题

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            int l_Index = 0;

            InitializeComponent();

            MenuItem[] l_MenuItems = new MenuItem[3];

            for (l_Index = 0; l_Index < l_MenuItems.Length; l_Index++)
            {
                l_MenuItems[l_Index] = new MenuItem("Menu " + l_Index.ToString(), MenuItem_Click);
            }

            System.Windows.Forms.ContextMenu l_ContextMenu = new ContextMenu(l_MenuItems);

            dataGridView1.ContextMenu = l_ContextMenu;
        }

        private void MenuItem_Click(object sender, EventArgs e)
        {
            MenuItem l_MenuItem = sender as MenuItem;

            if (l_MenuItem != null)
            {
                System.Windows.Forms.ContextMenu l_ContextMenu = l_MenuItem.GetContextMenu();

                if (l_ContextMenu != null)
                {
                    if (l_ContextMenu.SourceControl != null)
                    {
                        System.Diagnostics.Debug.WriteLine(l_ContextMenu.SourceControl.Name);
                    }
                }
            }
        }

        private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                dataGridView1.ContextMenu.Show(dataGridView1, new Point(e.X, e.Y));
            }
        }
    }
}

好的,首先,您的代码中有错误。 您的menuItem变量将始终为null ,因为发送者实际上是MenuItem ,因为您将ClearTable_Click分配给MenuItem而不是ContextMenu。

其次,为了从MenuItem获取ContextMenu,您需要查看VisualTree并搜索MenuItem的类型上下文菜单的父级。 有一个名为VisualTreeHelper.GetParent()的 API。 或者,您可以使用System.Windows.Interactivity.dll中的GetSelfAndAncestors帮助器方法:

using System.Windows.Interactivity;

private void ClearTable_Click(object sender, RoutedEventArgs e)
{
    var menuItem = (MenuItem)sender; //MenuItem that fired the click event
    var contextMenu = menuItem.GetSelfAndAncestors().OfType<ContextMenu>().First()
    UIElement sourceControl = menu.PlacementTarget;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM