找回密码
 立即注册

QQ登录

只需一步,快速开始

zhuxialiang

论坛元老

25

主题

75

帖子

1万

积分

论坛元老

积分
12575

活字格认证

zhuxialiang
论坛元老   /  发表于:2013-6-28 17:53  /   查看:6988  /  回复:5
如何实现WPF DataGrid鼠标右击选中行的效果。

5 个回复

倒序浏览
ZenosZeng讲师达人认证 悬赏达人认证
超级版主   /  发表于:2013-6-28 18:56:00
沙发
zhuxialiang 你好

我会在调查之后下周一给你回复结果。
回复 使用道具 举报
ZenosZeng讲师达人认证 悬赏达人认证
超级版主   /  发表于:2013-7-1 11:49:00
板凳
回复 1楼zhuxialiang的帖子

zhuxialiang 你好

在 C1DataGrid 中可以设置但行选择模式,代码如下,不知是否是你想要的效果:
c1DataGrid1.SelectionMode = C1.WPF.DataGrid.DataGridSelectionMode.SingleRow;
回复 使用道具 举报
ZenosZeng讲师达人认证 悬赏达人认证
超级版主   /  发表于:2013-7-1 11:57:00
地板
回复 1楼zhuxialiang的帖子

zhuxialiang 你好

如果需要通过鼠标右键来选中行可以使用以下代码:
  1.     public partial class MainWindow : Window
  2.     {
  3.         public MainWindow()
  4.         {
  5.             InitializeComponent();

  6.             c1DataGrid1.DataContext = GetData();
  7.             c1DataGrid1.CanUserAddRows = false;

  8.             c1DataGrid1.SelectionMode = C1.WPF.DataGrid.DataGridSelectionMode.SingleRow;

  9.             c1DataGrid1.LoadedRowPresenter += new EventHandler<C1.WPF.DataGrid.DataGridRowEventArgs>(c1DataGrid1_LoadedRowPresenter);            
  10.         }

  11.         void c1DataGrid1_LoadedRowPresenter(object sender, C1.WPF.DataGrid.DataGridRowEventArgs e)
  12.         {
  13.             e.Row.Presenter.PreviewMouseRightButtonDown += new MouseButtonEventHandler(Presenter_PreviewMouseRightButtonDown);
  14.         }

  15.         void Presenter_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
  16.         {
  17.             DataGridRowPresenter presenter = sender as DataGridRowPresenter;
  18.             c1DataGrid1.Selection.Add(presenter.Row, presenter.Row);
  19.         }

  20.         private System.Data.DataTable GetData()
  21.         {
  22.             System.Data.DataTable dt = new System.Data.DataTable();

  23.             dt.Columns.Add("ID");
  24.             dt.Columns.Add("Code");
  25.             dt.Columns.Add("Name");

  26.             dt.Rows.Add(1, "A10001", "Name_1");
  27.             dt.Rows.Add(2, "A10002", "Name_2");
  28.             dt.Rows.Add(3, "A10003", "Name_3");
  29.             dt.Rows.Add(4, "A10004", "Name_4");
  30.             dt.Rows.Add(5, "A10005", "Name_5");

  31.             return dt;
  32.         }
  33.     }
复制代码
回复 使用道具 举报
zhuxialiang
论坛元老   /  发表于:2013-7-1 12:47:00
5#
该问题已经解决,谢谢。
回复 使用道具 举报
ZenosZeng讲师达人认证 悬赏达人认证
超级版主   /  发表于:2013-7-1 14:40:00
6#
不客气
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部