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

WPF:获取DataGrid控件单元格DataGridCell的更多相关文章

  1. WPF 获取DataGrid 控件选中的单元格信息

    获取 DataGrid 选中的单元格的信息DataGridCellInfo cell_Info = this.studentTable.SelectedCells[0]; studentTableIt ...

  2. wpf 获取DataGrid某一个单元格,设置此单元格ToolTip内容和背景颜色

    public void GetCell()        {            for (int i = 0; i < this.datagrid1.Items.Count; i++)    ...

  3. WPF的DataGrid控件从excel里复制数据然后粘贴

    WPF的DataGrid控件不能像winform的DataGridView控件一样,支持值的粘贴.WPF的DataGrid控件本质上是跟数据绑定联系在一起,所以需要进行复制粘贴的操作,可以在wpf里用 ...

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

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

  5. WPF 4 DataGrid 控件(进阶篇一)

    原文:WPF 4 DataGrid 控件(进阶篇一)      上一篇<WPF 4 DataGrid 控件(自定义样式篇)>中,我们掌握了DataGrid 列表头.行表头.行.单元格相关的 ...

  6. WPF 4 DataGrid 控件(基本功能篇)

    原文:WPF 4 DataGrid 控件(基本功能篇)      提到DataGrid 不管是网页还是应用程序开发都会频繁使用.通过它我们可以灵活的在行与列间显示各种数据.本篇将详细介绍WPF 4 中 ...

  7. WPF 4 DataGrid 控件(进阶篇二)

    原文:WPF 4 DataGrid 控件(进阶篇二)      上一篇<WPF 4 DataGrid 控件(进阶篇一)>中我们通过DataGridTemplateColumn 类自定义编辑 ...

  8. WPF获取某控件的位置,也就是偏移量

    原文:WPF获取某控件的位置,也就是偏移量 此段示例在MSDN中可见.XAML代码如下: <Window xmlns="http://schemas.microsoft.com/win ...

  9. WPF 自定义DataGrid控件样式

    内容转自https://www.cnblogs.com/xiaogangqq123/archive/2012/05/07/2487166.html 一.DataGrid基本样式(一) 小刚已经把Dat ...

随机推荐

  1. 基础知识(C#语法、数据库SQL Server)回顾与总结

    前言 已经有大概一个多月没有更新博客,可能是开始变得有点懒散了吧,有时候想写,但是又需要额外投入更多的时间去学习,感觉精力完全不够用啊,所以为了弥补这一个多月的潜水,决定写一篇,衔接9月未写博客的空缺 ...

  2. 201521123084 《Java程序设计》第7周学习总结

    第7周-集合 1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 参考资料: XMind ------------------------------------------- ...

  3. 201521123107 《Java程序设计》第6周学习总结

    第6周作业-接口.内部类与Swing 1.本周学习总结 2.书面作业 1.clone方法 1.1 Object对象中的clone方法是被protected修饰,在自定义的类中覆盖clone方法时需要注 ...

  4. 学习目标或者作业的制定(SMART原则)

    以下文字摘自邹欣老师的博客 很高兴看到学生们都写了自己的目标: http://www.cnblogs.com/deng201421123059/p/6435346.html 不得不说,有些同学的目标太 ...

  5. Java 课程设计 "Give it up"小游戏设计(个人) 201521123077

    1.团队课程设计博客链接 http://www.cnblogs.com/agts/p/7067948.html 2.个人负责模块或任务说明 个人任务:实现游戏画面的制作,游戏逻辑的编写,以及一些模块与 ...

  6. 201521123063 《Java程序设计》 第9周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己以前 ...

  7. 201521123062《Java程序设计》第12周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象(属性:int id, String name,int age,doubl ...

  8. 201521123032 《Java程序设计》第3周学习总结(编辑器修改后)

    本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识组织起来.请使用纸笔或者下面的工具画出本周学习到的知识点.截图或者拍照上传. 书面作业 代码阅读 ...

  9. 从java的开始,java概述,java配置环境变量

    一.java开发入门 java 概述   Java划分为三个技术平台:JavaSE(标准版,含Java基础类库),JavaEE(企业版,技术平台),JavaME(小型版,小型产品.嵌入式设备) Jav ...

  10. 从JS和jQuery浅谈DOM操作,当我们在获取时,究竟获取了什么

    0.写在前面的话 自己对前端的东西一直不是很熟,现在开始要想办法从前端各个地方去获取想要的属性值的时候,也基本是在网上现炒现卖,几周下来,发现自己还是迷迷糊糊,可以算是一无所获. 所以就抽时间,把这一 ...