• WPF datagrid设置指定单元格的背景色和Enable


    效果图:

    添加扩展类

    public static class DataGridExtend
    
        {
    
            /// <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;
    
            }
    
        }

    用法:

     var cell = grid.GetCell(2,3);
                if (cell != null)
                {
                    cell.IsEnabled = false;
                    cell.Background = Brushes.LightGray;
                }

    以上是设置datagrid第三行第四列单元格的背景色和Enable属性。

  • 相关阅读:
    性能测试_LR11_数据向导
    python django部署linux服务器
    pip升级问题
    解决win10命令提示行下cnpm无反应
    adb常用命令连接设备/查看包名/查看activity
    android虚拟机sdcard操作出现mkdir failed for , Read-only file system的解决办法
    Django项目和应用创建
    mac下配置python虚拟环境
    这个是我的标题_2020_01_01_18_49_21
    这个是我的标题_2020_01_01_18_47_58
  • 原文地址:https://www.cnblogs.com/czly/p/13999412.html
Copyright © 2020-2023  润新知