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

时间:2015-08-24 13:57:57

标签: c# wpf

我需要获取启动事件的控件的名称,特别是我为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方法,所以这对我来说是一个问题。有办法解决这个或其他方式吗?

3 个答案:

答案 0 :(得分:2)

试试这个:

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

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

答案 1 :(得分:0)

将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...
        }
    }
}

希望这有帮助

答案 2 :(得分:0)

我创建了一个带有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));
            }
        }
    }
}