WPF 获得DataGridRow和 DataGridCell的方法

简介: 原文:WPF 获得DataGridRow和 DataGridCell的方法 原文地址 简介   在WPF中,DataGrid控件并没有提供访问其DataGridRow或者DataGridCell的方法。
原文: WPF 获得DataGridRow和 DataGridCell的方法

原文地址

简介

  在WPF中,DataGrid控件并没有提供访问其DataGridRow或者DataGridCell的方法。
  因此我们需要自己来编写获取的方法,这其中主要用到了ItemsControl类的一个实例方法:ItemContainerGenerator

实现代码

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;
        }
    }
}
目录
相关文章
|
C# Windows
WPF 获取程序路径的一些方法,根据程序路径获取程序集信息
原文:WPF 获取程序路径的一些方法,根据程序路径获取程序集信息 一、WPF 获取程序路径的一些方法方式一 应用程序域 //获取基目录即当前工作目录 string str_1 = System.
1620 0
|
C#
WPF中Binding使用StringFormat格式化字符串方法
原文:WPF中Binding使用StringFormat格式化字符串方法 货币格式 // $123.46 货币格式,一位小数 // $123.5 前文字 //单价:$123.
2197 0
|
C# Windows
WPF的WindowsFormsHost上浮动控件方法
如何在WPF的WindowsFormsHost实现浮动控件
208 0
|
C# 虚拟化 UED
细数改善WPF应用程序性能的10大方法
原文:细数改善WPF应用程序性能的10大方法       WPF(Windows Presentation Foundation)应用程序在没有图形加速设备的机器上运行速度很慢是个公开的秘密,给用户的感觉是它太吃资源了,WPF程序的性能和硬件确实有很大的关系,越高档的机器性能越有优势。
1135 0
|
编解码 C#
WPF中三种方法得到当前屏幕的宽和高
原文:WPF中三种方法得到当前屏幕的宽和高 WPF程序中的单位是与设备无关的单位,每个单位是1/96英寸,如果电脑的DPI设置为96(每个英寸96个像素),那么此时每个WPF单位对应一个像素,不过如果电脑的DPI设备为120(每个英寸120个像素),那此时每个WPF单位对应应该是120/96=1.
1139 0
|
C#
WPF简单实用方法(持续更新)
原文:WPF简单实用方法(持续更新)   1:点击退出提示框 MessageBoxResult result = MessageBox.Show("你真的要退出吗?", "", MessageBoxButton.
524 0
|
C#
在WPF下快速生成线的方法
原文:在WPF下快速生成线的方法 如果线较多时,在画布中用Path或Line生成时会比较慢。用DrawingVisual可以快速生成,这个在之前我的博客中已经提到。但在类库形式下生成的无法看到,保存成Image后再加入图层后成功显示。
1008 0
|
C#
WPF DataGrid 每行ComboBox 内容不同的设置方法
原文:WPF DataGrid 每行ComboBox 内容不同的设置方法 ...
1049 0
|
C#
WPF在DLL中读取Resource的方法
原文:WPF在DLL中读取Resource的方法 WPF是个用户控件,被WinForm调用。而WinForm是在一个DLL类库中被调用。试了很多方法,都无法将Resource中的图读进程序。用下面的方法总算实现了。
1749 0
|
C#
WPF获取窗口句柄的方法
原文:WPF获取窗口句柄的方法 通过WPF的互操作帮助类WindowInteropHelper,相关连接:https://msdn.microsoft.com/zh-cn/library/system.
1621 0