当前位置: 首页 > news >正文

wpf datagrid通过点击单元格 获取行列索引2.0

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

http://www.mrgr.cn/news/13997.html

相关文章:

  • 【PGCCC】揭秘PostgreSQL内存表的隐形翅膀:深入探讨索引的原理与实现
  • DHT11 实现温湿度传感器
  • 光庭信息半年报:营收利润「双」下降,汽车软件业务竞争加剧
  • polarctf靶场[WEB]Don‘t touch me、机器人、uploader、扫扫看
  • linux系统中内存和缓冲简介
  • EmguCV学习笔记 C# 第7章 特征点检测与匹配
  • 过滤器与拦截器对比
  • java基础 之 接口
  • Nginx负载均衡SSL证书配置全指南
  • Spring框架:从依赖注入到微服务
  • 使用Hutool操作Excel的时候出现的问题(压缩比问题)
  • ## 已解决:亲测有效的 `java.nio.charset.CoderMalfunctionError` 编码器故障错误解决方法
  • Web大学生网页作业成品——VIVO介绍网页设计与实现(HTML+CSS)(1个页面)
  • 【2024年】为Python股票量化分析最新整理的免费股票数据API接口之历史数据
  • 服务器远程管理
  • C++ 设计模式——组合模式
  • 1万2千多条电工考试题库ACCESS\EXCEL数据库
  • 不良信息公示
  • 【小沐学Rust】Rust实现TCP网络通信
  • 低代码用户中心的构建与应用