• using System.Windows.Controls;
  • using System.Windows.Controls.Primitives;
  • using System.Windows.Media;
  • namespace Splash.WPF
  • {
  • public static class DataGridPlus
  • {
  • /// <summary>
  • /// 获取DataGrid控件单元格
  • /// </summary>
  • /// <param name="dataGrid">DataGrid控件</param>
  • /// <param name="rowIndex">单元格所在的行号</param>
  • /// <param name="columnIndex">单元格所在的列号</param>
  • /// <returns>指定的单元格</returns>
  • public static DataGridCell GetCell(this DataGrid dataGrid, int rowIndex, int columnIndex)
  • {
  • DataGridRow rowContainer = dataGrid.GetRow(rowIndex);
  • if (rowContainer != null)
  • {
  • DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
  • DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
  • if (cell == null)
  • {
  • dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[columnIndex]);
  • cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
  • }
  • return cell;
  • }
  • return null;
  • }
  • /// <summary>
  • /// 获取DataGrid的行
  • /// </summary>
  • /// <param name="dataGrid">DataGrid控件</param>
  • /// <param name="rowIndex">DataGrid行号</param>
  • /// <returns>指定的行号</returns>
  • public static DataGridRow GetRow(this DataGrid dataGrid, int rowIndex)
  • {
  • DataGridRow rowContainer = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
  • if (rowContainer == null)
  • {
  • dataGrid.UpdateLayout();
  • dataGrid.ScrollIntoView(dataGrid.Items[rowIndex]);
  • rowContainer = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
  • }
  • return rowContainer;
  • }
  • /// <summary>
  • /// 获取父可视对象中第一个指定类型的子可视对象
  • /// </summary>
  • /// <typeparam name="T">可视对象类型</typeparam>
  • /// <param name="parent">父可视对象</param>
  • /// <returns>第一个指定类型的子可视对象</returns>
  • public static T GetVisualChild<T>(Visual parent) where T : Visual
  • {
  • T child = default(T);
  • int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
  • for (int i = 0; i < numVisuals; i++)
  • {
  • Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
  • child = v as T;
  • if (child == null)
  • {
  • child = GetVisualChild<T>(v);
  • }
  • if (child != null)
  • {
  • break;
  • }
  • }
  • return child;
  • }
  • }
  • }

遍历WPF DataGrid单元格的更多相关文章

  1. 关于C# wpf DataGrid单元格双击设置单元格内容

    1.我是使用了 visual stadio 2015, 用的C# WPF写个工具,但是发现wpf原生没有涉及表格的东西(类似 winform·的DataGridView),所以使用的是toolkit工 ...

  2. WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决

    原文:WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决 如下图,在凭证编辑窗体中,有的单元格不需要数字,但如果录入数字后再删除,会触发数字验证,单元格显示红色框线,导致不能执行 ...

  3. 基于1.3.3版本tooltip的datagrid单元格tip实现

    基于1.3.3版本tooltip的datagrid单元格tip实现 2013年05月25日 ⁄ datagrid ⁄ 共 6122字 ⁄ 评论数 26 ⁄ 被围观 7,033 views+ 文章目录 ...

  4. datagrid单元格格式化样式化

    本文体验datagrid单元格的格式化和样式化.   datagrid显示的DOM结构 <td field="code"> <div style="te ...

  5. WPF学习笔记(1):DataGrid单元格实现逐键过滤功能

    最近,开始学习WPF,其UI设计完全颠覆了传统的设计理念,为程序员提供了极大的自由发挥空间,让我为之惊叹,且为之着迷.然而,WPF在国内的热度却并不高,大部分贴子都是2012年以前的,出版的图书也很少 ...

  6. WPF中修改DataGrid单元格值并保存

    编辑DataGrid中的单元格的内容然后保存是非常常用的功能.主要涉及到的方法就是DataGrid的CellEditEnding  和BeginningEdit .其中BeginningEdit 是当 ...

  7. EasyUI datagrid单元格文本超出显示省略号,鼠标移动到单元格显示文本

    nowrap : true;  是前提 $('#×××').datagrid({ nowrap : true,//设置为true,当数据长度超出列宽时将会自动截取 }); 省略号样式: <sty ...

  8. easyui datagrid 单元格编辑(cell editing)

    demo中有row editing 项目中发现个cell editing,但是有bug,修改好了 主要实现功能:单击数据表格单元格,编辑单元格数据 js代码如下: $.extend($.fn.data ...

  9. EasyUI Datagrid 单元格编辑

    3:对于单元格的编辑 $('#Units').datagrid({ pageNumber: 1, //url: "@ViewBag.Domain/Paper/GetQuestionUnit& ...

随机推荐

  1. MHA的MySQL高可用方案实战

    功能: 1)master的故障切换(keepalived VIP的飘移) 2)主从复制角色的提升和重新转向 其中master 对外提供写服务,备选master2(实际的slave提供读服务,slave ...

  2. js的StringBuffer类

    function StringBuffer(str){ var arr = []; str = str || ""; arr.push(str); this.append = fu ...

  3. 几校联考——day1题解

    T1 约数的个数(好像不可提交) 如果一个整数a能够整除整数b,那么a叫做b的约数.现在有N(1 <= N <= 100,000)个整数,对于其中的每一个数,请找出它在其余N - 1个整数 ...

  4. ELK6 收集不同来源的日志并做区分

    https://blog.csdn.net/u010871982/article/details/79035317 使用filebeat替代logstash进行日志采集 https://blog.cs ...

  5. PHP websocket之聊天室实现

    PHP部分 <?php error_reporting(E_ALL); set_time_limit(0);// 设置超时时间为无限,防止超时 date_default_timezone_set ...

  6. 小白两篇博客熟练操作MySQL 之 第二篇

    小白两篇博客熟练操作MySQL  之   第二篇 一. 视图 视图是一个虚拟表,其本质是根据SQL语句获取动态的数据集,并为其命名,用户使用时只需使用名称即可获取结果集, 并可以将其当做表来使用. s ...

  7. VS2017git 提交提示错误 Git failed with a fatal error.

    具体错误信息:Git failed with a fatal error.error: open("ConsoleApp1/.vs/ConsoleApp1/v15/Server/sqlite ...

  8. SRPING MVC基本配置

    作下记录,WEB.XML的配置及DispatcherServlet-context.xml的配置. 而后者的配置,有不同的形式,不要被迷惑. WEB.XML <servlet> <s ...

  9. 洛谷—— P2733 家的范围 Home on the Range

    https://www.luogu.org/problem/show?pid=2733 题目背景 农民约翰在一片边长是N (2 <= N <= 250)英里的正方形牧场上放牧他的奶牛.(因 ...

  10. 洛谷 P1796 汤姆斯的天堂梦_NOI导刊2010提高(05)

    P1796 汤姆斯的天堂梦_NOI导刊2010提高(05) 题目描述 汤姆斯生活在一个等级为0的星球上.那里的环境极其恶劣,每天12小时的工作和成堆的垃圾让人忍无可忍.他向往着等级为N的星球上天堂般的 ...