c# - 右键单击数据网格以显示上下文菜单 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 - C程序计算字符串中字符的出现次数
- c# - WPF 的 PasswordBox 中的 SecureString 与手动构建的不同
- c# - Unity2D射击目标以随机颜色匹配 - C#
- android-studio - 使用 ndk-build 的 Android Studio 本机 lib 构建错误
- arrays - Swift Alamofire 将值放入字符串
- java - for Loop 2d 数组 BufferedWriter 未写入 txt
- angular - 为什么没有完整部署的 Angular Firebase Cloud Functions Emulator 不会更新函数?
- apache-spark - SparkSQL 无法将数据放入内存
- xamarin - 如何将 MVVMCross6 与 Xamarin.Forms 一起使用
- ruby-on-rails - ActiveAdmin - 如何通过 AJAX 向表单提交按钮发送 http 请求