1. /* ----------------------------------------------------------
  2. 文件名称:DataGridPlus.cs
  3.  
  4. 作者:秦建辉
  5.  
  6. MSN:splashcn@msn.com
  7. QQ:36748897
  8.  
  9. 博客:http://blog.csdn.net/jhqin
  10.  
  11. 开发环境:
  12. Visual Studio V2010
  13. .NET Framework 4 Client Profile
  14.  
  15. 版本历史:
  16. V1.0 2012年06月07日
  17. WPF DataGrid控件扩展方法
  18.  
  19. 参考资料:
  20. http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b7299e55-92e2-4a6b-8987-869fef8f22eb/
  21. ------------------------------------------------------------ */
  22. using System.Windows.Controls;
  23. using System.Windows.Controls.Primitives;
  24. using System.Windows.Media;
  25.  
  26. namespace Splash.WPF
  27. {
  28. public static class DataGridPlus
  29. {
  30. /// <summary>
  31. /// 获取DataGrid控件单元格
  32. /// </summary>
  33. /// <param name="dataGrid">DataGrid控件</param>
  34. /// <param name="rowIndex">单元格所在的行号</param>
  35. /// <param name="columnIndex">单元格所在的列号</param>
  36. /// <returns>指定的单元格</returns>
  37. public static DataGridCell GetCell(this DataGrid dataGrid, int rowIndex, int columnIndex)
  38. {
  39. DataGridRow rowContainer = dataGrid.GetRow(rowIndex);
  40. if (rowContainer != null)
  41. {
  42. DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
  43. DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
  44. if (cell == null)
  45. {
  46. dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[columnIndex]);
  47. cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
  48. }
  49. return cell;
  50. }
  51. return null;
  52. }
  53.  
  54. /// <summary>
  55. /// 获取DataGrid的行
  56. /// </summary>
  57. /// <param name="dataGrid">DataGrid控件</param>
  58. /// <param name="rowIndex">DataGrid行号</param>
  59. /// <returns>指定的行号</returns>
  60. public static DataGridRow GetRow(this DataGrid dataGrid, int rowIndex)
  61. {
  62. DataGridRow rowContainer = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
  63. if (rowContainer == null)
  64. {
  65. dataGrid.UpdateLayout();
  66. dataGrid.ScrollIntoView(dataGrid.Items[rowIndex]);
  67. rowContainer = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
  68. }
  69. return rowContainer;
  70. }
  71.  
  72. /// <summary>
  73. /// 获取父可视对象中第一个指定类型的子可视对象
  74. /// </summary>
  75. /// <typeparam name="T">可视对象类型</typeparam>
  76. /// <param name="parent">父可视对象</param>
  77. /// <returns>第一个指定类型的子可视对象</returns>
  78. public static T GetVisualChild<T>(Visual parent) where T : Visual
  79. {
  80. T child = default(T);
  81. int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
  82. for (int i = ; i < numVisuals; i++)
  83. {
  84. Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
  85. child = v as T;
  86. if (child == null)
  87. {
  88. child = GetVisualChild<T>(v);
  89. }
  90. if (child != null)
  91. {
  92. break;
  93. }
  94. }
  95. return child;
  96. }
  97. }
  98. }

DataGrid 得到DataGridRow 和DataGridColumn的更多相关文章

  1. WPF DataGrid显格式

    Guide to WPF DataGrid formatting using bindings Peter Huber SG, 25 Nov 2013 CPOL    4.83 (13 votes) ...

  2. WPF DataGrid Custommization using Style and Template

    WPF DataGrid Custommization using Style and Template 代码下载:http://download.csdn.net/detail/wujicai/81 ...

  3. WPF 4 DataGrid 控件(自定义样式篇)

    原文:WPF 4 DataGrid 控件(自定义样式篇)      在<WPF 4 DataGrid 控件(基本功能篇)>中我们已经学习了DataGrid 的基本功能及使用方法.本篇将继续 ...

  4. datagridcolumn单元格怎么显示查询到的某个表的字段值(字段值可能为多个)

    例如,在之前做的项目中,查询mhz_xckcr表,select出某个业务的现场勘察人信息,select出的现场勘察人姓名(可能有多个)要在前台datagrid的一个datagridcolmn单元格显示 ...

  5. WPF之DataGrid的LoadingRow事件

    利用DataGrid的LoadingRow事件,可以进行设置DataGrid的DataGridRow的属性(例如样式:背景色.前景色:是否可修改数据) 实例代码如下: private void Dat ...

  6. datagrid和combobox简单应用

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...

  7. DataGrid 滚动特定的行或者列

    DataGrid 滚动特定的行或者列. DataGrid.ScrollIntoView Method (Object, DataGridColumn) .NET Framework 4.5 Silve ...

  8. DataGrid 獲取 制定 row Col 單元格

    public static class DataGridHelper     {         /// <summary>                  /// Gets the v ...

  9. jQuery DataTables 使用手册(精简版)

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/5182940.html 前排提醒,这个插件能不用就不用,那么多好的插件等着你,为什么要用它呢?就算用easyui的 ...

随机推荐

  1. 如何在C++中产生随机数

    C++中没有自带的random函数,要实现随机数的生成就需要使用rand()和srand().不过,由于rand()的内部实现是用线性同余法做的,所以生成的并不是真正的随机数,而是在一定范围内可看为随 ...

  2. 201521123079《java程序设计》第8周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 本次作业题集集合 1.List中指定元素的删除(题目4-1) 1.1 实验总结 这题主要是对函 ...

  3. 201521123016 《Java程序设计》第2周学习总结

    1. 本周学习总结 JAVA中string对象创建后不可修改. 使用StringBuilder编写代码,减少内存空间的占用. 字符串使用"+"拼接,拼接后其他类型会被转化为字符串. ...

  4. 201521123056 《Java程序设计》第1周学习总结

    1. 本周学习总结 java语言结构简单,但功能齐全,同时能够在不同系统平台上编译,但编译的前提是系统要有安装JVM(即java虚拟机),JVM是java实现跨平台的最核心部分.本周还学习了JDK.J ...

  5. 201521123093 java 第十四周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多数据库相关内容. MySQL中的库操作和表操作 库操作: 显示所有数据库: show databases; 创建数据库: crea ...

  6. 201521123109《java程序设计》第十周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 2. 书面作业 本次PTA作业题集异常.多线程 1. finally 题目4-2 1.1 截图你的提交结果( ...

  7. 201521123066 《Java程序设计》第十一周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 1.互斥访问与同步访问 完成题集4-4(互斥访问)与4-5(同步访问) 1.1 除了使用synch ...

  8. java:接口特性 接口与抽象类/普通类的区别

    接口 书面定义: Java接口是一系列方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现,因此这些方法可以在不同的地方被不同的类实现,而这些实现可以具有不同的行为(功能). 在ja ...

  9. Sql Server——基础

    前言: 在了解数据库之前,我们应该首先了解一下和数据库有关的知识,如:什么是数据,什么又是数据库等.  数据:描述事物的符号记录称为数据,它是数据库中存储的基本对象.  数据库(Datebase):数 ...

  10. Spring配置属性文件

    在项目开发阶段和交付阶段数据库的连接信息往往是不同的,可以把这些信息写成属性文件,再在Spring中导入即可引用 jdbc.properties属性文件如下: jdbc.driverClassName ...