首页 > 解决方案 > 右键单击数据网格以显示上下文菜单 wpf

问题描述

我有一个数据网格,我希望能够右键单击,使其显示一个弹出窗口并捕获它已被右键单击的行。我在这里发现了一些非常相似的东西:右键单击 datagridview 的上下文菜单,但它们都使用了,DataGridViewCellMouseEventArgs因为它们很旧。我找到了一个相对较新的,它是这样的:

if (e.ChangedButton == MouseButton.Right)
        {
            System.Windows.Forms.ContextMenu m = new System.Windows.Forms.ContextMenu();
            m.MenuItems.Add(new System.Windows.Forms.MenuItem("Cut"));
            m.MenuItems.Add(new System.Windows.Forms.MenuItem("Copy"));
            m.MenuItems.Add(new System.Windows.Forms.MenuItem("Paste"));

            int currentMouseOverRow = datagrid.HitTest(e.X, e.Y).RowIndex;

            if (currentMouseOverRow >= 0)
            {
                m.MenuItems.Add(new System.Windows.Forms.MenuItem(string.Format("Do something to row {0}", currentMouseOverRow.ToString())));
            }

            m.Show(datagrid, new Point(e.X, e.Y));

        }

但是我在m.Show(datagrid, new Point(e.X, e.Y));and上遇到错误HitTest。任何帮助,将不胜感激。

标签: c#wpfdatagrid

解决方案


推荐阅读