private void DataGrid_OnMouseRightButtonUp(object sender, MouseButtonEventArgs e)
{var dataGrid = sender as DataGrid;// 获取点击的位置var mousePosition = e.GetPosition(dataGrid);var hit = dataGrid.InputHitTest(mousePosition) as FrameworkElement;if (hit != null){// 获取点击的单元格DataGridCell cell = FindParent<DataGridCell>(hit);if (cell != null){DataGridRow row = FindParent<DataGridRow>(cell);// 获取行索引int rowIndex = row.GetIndex();//获取列索引int columnIndex = cell.Column.DisplayIndex;Console.WriteLine($"行索引: {rowIndex}, 列索引: {columnIndex}");// 获取当前单元格的内容// var currentValue = cell.Content.ToString(); // 修改数据源中的值dynamic rowItem = dataGrid.Items[rowIndex];var propertyName = cell.Column.SortMemberPath; // 获取绑定的属性名rowItem.GetType().GetProperty(propertyName).SetValue(rowItem, "aaa");}}
}// 辅助方法:找到指定类型的父级元素
private T FindParent<T>(DependencyObject child) where T : DependencyObject
{DependencyObject parentObject = VisualTreeHelper.GetParent(child);if (parentObject == null) return null;T parent = parentObject as T;return parent ?? FindParent<T>(parentObject);
}wpf datagrid通过点击单元格 获取行列索引2.0
2025/11/28 6:08:46
来源:https://blog.csdn.net/u014018281/article/details/141558190
浏览:
次
关键词:wpf datagrid通过点击单元格 获取行列索引2.0
版权声明:
本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。
我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com
热文排行
最新新闻
- 【自学笔记】Hadoop基础知识点总览-持续更新
- Python高效处理海量气溶胶数据;MODIS和CALIPSO气溶胶数据下载、处理与反演分析
- 大数据开发语言Scala(一) - Scala入门
- 蓝桥杯物联网竞赛_STM32L071_20_用printf将数据显示在OLED上
- 初识Linux · 命名管道
- Docker部署tenine实现后端应用的高可用与负载均衡
- 构建前瞻性的AI战略,助力企业数字财务发展
- 【BES2500x系列 -- RTX5操作系统】Battery模块 -- 邮箱线程 Battery 钩子函数注册 --(十四)
- 韩顺平老师Python学习笔记【持续更新...】
- Git 克隆 GitHub 仓库时遇到了 SSL 证书问题
推荐新闻
- 【自学笔记】Hadoop基础知识点总览-持续更新
- Python高效处理海量气溶胶数据;MODIS和CALIPSO气溶胶数据下载、处理与反演分析
- 大数据开发语言Scala(一) - Scala入门
- 蓝桥杯物联网竞赛_STM32L071_20_用printf将数据显示在OLED上
- 初识Linux · 命名管道
- Docker部署tenine实现后端应用的高可用与负载均衡
- 构建前瞻性的AI战略,助力企业数字财务发展
- 【BES2500x系列 -- RTX5操作系统】Battery模块 -- 邮箱线程 Battery 钩子函数注册 --(十四)
- 韩顺平老师Python学习笔记【持续更新...】
- Git 克隆 GitHub 仓库时遇到了 SSL 证书问题
