如何获取已启动事件的控件的名称

本文关键字:事件 控件 启动 何获取 获取 | 更新日期: 2023-09-27 18:09:27

我需要获得启动事件的控件的名称,特别是我为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
    }
}

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

一个示例,将sender对象作为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));
            }
        }
    }
}

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

第二,为了从MenuItem中获得ContextMenu,你需要查看VisualTree并搜索MenuItem的父类型context menu。有一个API叫做VisualTreeHelper.GetParent()。或者,您可以使用system . windows . interactive .dll中的GetSelfAndAncestors helper方法:

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;
}