原文:WPF 获得DataGridRow和 DataGridCell的方法

原文地址

简介

  在WPF中,DataGrid控件并没有提供访问其DataGridRow或者DataGridCell的方法。

  因此我们需要自己来编写获取的方法,这其中主要用到了ItemsControl类的一个实例方法:ItemContainerGenerator。

实现代码

  1. using System.Windows.Controls;
  2. using System.Windows.Controls.Primitives;
  3. using System.Windows.Media;
  4. namespace Splash.WPF
  5. {
  6. public static class DataGridPlus
  7. {
  8. /// <summary>
  9. /// 获取DataGrid控件单元格
  10. /// </summary>
  11. /// <param name="dataGrid">DataGrid控件</param>
  12. /// <param name="rowIndex">单元格所在的行号</param>
  13. /// <param name="columnIndex">单元格所在的列号</param>
  14. /// <returns>指定的单元格</returns>
  15. public static DataGridCell GetCell(this DataGrid dataGrid, int rowIndex, int columnIndex)
  16. {
  17. DataGridRow rowContainer = dataGrid.GetRow(rowIndex);
  18. if (rowContainer != null)
  19. {
  20. DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
  21. DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
  22. if (cell == null)
  23. {
  24. dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[columnIndex]);
  25. cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
  26. }
  27. return cell;
  28. }
  29. return null;
  30. }
  31. /// <summary>
  32. /// 获取DataGrid的行
  33. /// </summary>
  34. /// <param name="dataGrid">DataGrid控件</param>
  35. /// <param name="rowIndex">DataGrid行号</param>
  36. /// <returns>指定的行号</returns>
  37. public static DataGridRow GetRow(this DataGrid dataGrid, int rowIndex)
  38. {
  39. DataGridRow rowContainer = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
  40. if (rowContainer == null)
  41. {
  42. dataGrid.UpdateLayout();
  43. dataGrid.ScrollIntoView(dataGrid.Items[rowIndex]);
  44. rowContainer = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
  45. }
  46. return rowContainer;
  47. }
  48. /// <summary>
  49. /// 获取父可视对象中第一个指定类型的子可视对象
  50. /// </summary>
  51. /// <typeparam name="T">可视对象类型</typeparam>
  52. /// <param name="parent">父可视对象</param>
  53. /// <returns>第一个指定类型的子可视对象</returns>
  54. public static T GetVisualChild<T>(Visual parent) where T : Visual
  55. {
  56. T child = default(T);
  57. int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
  58. for (int i = 0; i < numVisuals; i++)
  59. {
  60. Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
  61. child = v as T;
  62. if (child == null)
  63. {
  64. child = GetVisualChild<T>(v);
  65. }
  66. if (child != null)
  67. {
  68. break;
  69. }
  70. }
  71. return child;
  72. }
  73. }
  74. }

WPF 获得DataGridRow和 DataGridCell的方法的更多相关文章

  1. WPF 获取程序路径的一些方法,根据程序路径获取程序集信息

    一.WPF 获取程序路径的一些方法方式一 应用程序域 //获取基目录即当前工作目录 string str_1 = System.AppDomain.CurrentDomain.BaseDirector ...

  2. WPF多线程UI更新——两种方法

    WPF多线程UI更新——两种方法 前言 在WPF中,在使用多线程在后台进行计算限制的异步操作的时候,如果在后台线程中对UI进行了修改,则会出现一个错误:(调用线程无法访问此对象,因为另一个线程拥有该对 ...

  3. WPF Popup全屏 弹出方法。解决只显示75%的问题。

    WPF Popup全屏 弹出方法.解决只显示75%的问题.   WPF 中 Popup 有一个特点.当Popup的高度超过屏幕的75%的时候,只显示75%的高度. 如下代码: <Window x ...

  4. 解决VS2019中.net core WPF 暂时无法使用 Designer 的临时方法

    目录 解决 VS2019 中.net core WPF 暂时无法使用 Designer 的临时方法 安装 vs 2019 professional/enterprise版本 在vs的设置里,勾选.NE ...

  5. WPF PasswordBox不支持绑定解决方法

    原文:WPF PasswordBox不支持绑定解决方法 PasswordBox的Password属性因为安全原因不支持直接绑定,可以使用依赖属性实现.直接插入代码 public class Passw ...

  6. WPF 3D模型的一个扩展方法

    原文:WPF 3D模型的一个扩展方法 在WPF 3D中,我们常常需要改变一个ModelVisual3D对象的颜色. 先说说ModelVisual3D,本质上3D模型都是由一个个的三角形构成的,并且经过 ...

  7. WPF窗体隐藏鼠标光标的方法

    原文:WPF窗体隐藏鼠标光标的方法 要引用 System.Windows.Input;   Mouse.OverrideCursor = Cursors.None; 去掉 Override 则使用: ...

  8. WPF显示html的几种方法

    原文:WPF显示html的几种方法 客户希望系统在一些特定的条件下,界面上能显示用户自定义的格式和内容,格式和内容通过html指定. 基本上在wpf中显示html有以下四种方法. 1.       W ...

  9. WPF 获取系统 DPI 的多种方法

    原文:WPF 获取系统 DPI 的多种方法 WPF 获取系统 DPI 的多种方法 由于 WPF 的尺寸单位和系统的 DPI 相关,我们有时需要获取 DPI 值来进行一些界面布局的调整,本文汇总了一些 ...

随机推荐

  1. vs 外部依赖项、附加依赖项以及如何添加依赖项目

    我们在 VS 中创建 Win32 控制台应用程序,vs 会为解决方案创建默认地创建 4 个 filters(资源管理器中没有对应的目录和文件夹): 头文件:一般为 .h 文件 外部依赖项 源文件:一般 ...

  2. iis MP4 不能访问404

    为什么我上传了flv或MP4文件到服务器,可输入正确地址通过http协议来访问总是出现“无法找到该页”的404错误呢?这就表明mp4格式文件是服务器无法识别的,其实,这是没有在iis中将相应的MIME ...

  3. CEPH OBJECTSTORE API介绍

    Thomas是本人在Ceph中国社区翻译小组所用的笔名,该文首次公布在Ceph中国社区.现转载到本人博客,以供大家传阅 CEPH OBJECTSTORE API介绍 本文由 Ceph中国社区-Thom ...

  4. Android中的动画详解系列【1】——逐帧动画

    逐帧动画其实很简单,下面我们来看一个例子: <?xml version="1.0" encoding="utf-8"?> <animation ...

  5. 使用Redis做产品统计的两种模式

    http://zihua.li/2012/07/two-patterns-of-statistics-using-redis/ 产品运行过程中及时记录收集并分析统计数据对产品的持续改进有重要的指导作用 ...

  6. svn X在Xcode中使用

    1 在终端输入命令:清除以前的svn链接地址( /Users/mac/Desktop/SHiosProject/SVNmangerfiles) nie-xiao-bo-mac-pro:~ mac$ s ...

  7. Swagger 专题

    随着互联网技术的发展,现在的网站架构基本都由原来的后端渲染,变成了:前端渲染.前后端分离的形态,而且前端和后端在各自的技术道路上越走越远. 前端和后端的唯一联系,变成了API接口:API文档成了前后端 ...

  8. MapReduce 经典案例手机流量排序的分析

    在进行流量排序之前,先要明白排序是发生在map阶段,排序之后(排序结束后map阶段才会显示100%完成)才会到reduce阶段(事实上reduce也会排序),.此外排序之前要已经完成了手机流量的统计工 ...

  9. xml报错(dtd):The markup declarations contained or pointed to by the document type declaration must be well-formed

    文件后缀为.xml里如下一行报错“The markup declarations contained or pointed to by the document type declaration mu ...

  10. Ibatis之RowHandler

    如果一个场景:账户表中有1千万账户,现在,我们需要这个1千万账户利息结算业务.需求是基于Ibatis框架来实现这个功能. 如果按照一般的编程模式,我们将编写一个sql,然后调用QueryForList ...