• WPF:获取DataGrid控件单元格DataGridCell


    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;  
            }  
        }  
    } 
  • 相关阅读:
    《深入理解Java虚拟机》笔记--第四章、虚拟机性能监控与故障处理工具
    《深入理解Java虚拟机》笔记--第三章 、垃圾收集器与内存分配策略
    《深入理解Java虚拟机》笔记--第二章、Java内存区域与内存溢出异常
    Netty并发优化之ExecutionHandler
    Netty框架入门
    123
    如何清理任务栏程序预览的历史记录?
    三个安装,看VIP电影
    WIN 10 COM surrogate 关闭
    微软官方win10系统安装u盘启动盘制作
  • 原文地址:https://www.cnblogs.com/happinesshappy/p/5019356.html
Copyright © 2020-2023  润新知