如何以编程方式禁用WPF DataGrid中的特定单元 [英] how to programatically disable a particular cell in WPF DataGrid

查看:106
本文介绍了如何以编程方式禁用WPF DataGrid中的特定单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF DataGrid。
您可以告诉,如何以编程方式禁用WPF DataGrid中的特定单元格。

解决方案

我回答了这个问题当我遇到同样的问题时,这是我提出的解决方案。



您无法直接在WPF中访问单元格和行,因此我们首先定义了一些辅助扩展。

使用以下代码: http:// techiethings.blogspot.com/2010/05/get-wpf-datagrid-row-and-cell.html

  public static class DataGridExtensions 
{
public static T GetVisualChild< T>(Visual parent)其中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; (DataGrid grid,int index)



公共静态DataGridRow GetRow(此DataGrid网格,int索引)
{
DataGridRow row =(DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
if(row == null)
{
//可以被虚拟化,显示并重试。
grid.UpdateLayout();
grid.ScrollIntoView(grid.Items [index]);
row =(DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
}
返回行;


public static DataGridCell GetCell(DataGrid grid,DataGridRow row,int column)
{
if(row!= null)
{
DataGridCellsPresenter演示者= GetVisualChild< DataGridCellsPresenter>(row);

if(presenter == null)
{
grid.ScrollIntoView(row,grid.Columns [column]);
presenter = GetVisualChild< DataGridCellsPresenter>(row);
}

DataGridCell cell =(DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
返回单元格;
}
返回null;

$ b $ public static DataGridCell GetCell(DataGrid grid,int row,int column)
{
DataGridRow gridRow = GetRow(grid,row);
返回GetCell(grid,gridRow,column);






$ b

通过这个我们可以得到第一行的单元格,第五列是这样的:

  dataGrid1.GetCell(0,4)

因此,将列设置为禁用现在很容易:

  dataGrid1.GetCell(0,4).IsEnabled = false; 

请注意在某些情况下,表单需要先载入任何这些作品。



希望这有助于某个人; - )


I m having a WPF DataGrid. Can u please tell , how to programatically disable a particular cell in WPF DataGrid.

解决方案

I'm answering this as I ran into the same issue, this is the solution I came up with.

You can't access cells and rows directly in WPF so we first define some helper extensions.

(Using some of the code from: http://techiethings.blogspot.com/2010/05/get-wpf-datagrid-row-and-cell.html)

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

    public static DataGridRow GetRow(this DataGrid grid, int index)
    {
        DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
        if (row == null)
        {
            // May be virtualized, bring into view and try again.
            grid.UpdateLayout();
            grid.ScrollIntoView(grid.Items[index]);
            row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
        }
        return row;
    }

    public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column)
    {
        if (row != null)
        {
            DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);

            if (presenter == null)
            {
                grid.ScrollIntoView(row, grid.Columns[column]);
                presenter = GetVisualChild<DataGridCellsPresenter>(row);
            }

            DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            return cell;
        }
        return null;
    }

    public static DataGridCell GetCell(this DataGrid grid, int row, int column)
    {
        DataGridRow gridRow = GetRow(grid, row);
        return GetCell(grid, gridRow, column);
    }
}

With this we can get the cell at the first row, fifth column like this:

dataGrid1.GetCell(0, 4)

So to set the column to disabled is now really easy:

dataGrid1.GetCell(0, 4).IsEnabled = false;

Please note In some cases it is necessary for the form to load before any of this works.

Hope this helps someone someday ;-)

这篇关于如何以编程方式禁用WPF DataGrid中的特定单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆