【问题标题】:how to programatically disable a particular cell in WPF DataGrid如何以编程方式禁用 WPF DataGrid 中的特定单元格
【发布时间】:2011-12-11 18:12:57
【问题描述】:

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

【问题讨论】:

    标签: c# .net wpf gridview


    【解决方案1】:

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

    您不能直接在 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) 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);
        }
    }
    

    这样我们可以得到第一行第五列的单元格,如下所示:

    dataGrid1.GetCell(0, 4)
    

    所以现在将列设置为禁用非常简单:

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

    请注意在某些情况下,必须先加载表单,然后才能进行任何操作。

    希望有一天这对某人有所帮助;-)

    【讨论】:

      【解决方案2】:

      使用样式,如下所示:

      <DataGrid.CellStyle>
          <Style TargetType="DataGridCell" >
              <Style.Setters>
                  <Setter Property="IsEnabled" Value="False"/>
              </Style.Setters>
          </Style>
      </DataGrid.CellStyle>
      

      【讨论】:

      • 我认为这对他/她没有用处。他/她想以编程方式禁用它们。
      • 这可用于通过使用值的绑定和转换器以编程方式设置 IsEnabled。 &lt;Setter Property="IsEnabled" Value="{Binding Index, Converter={StaticResource CellEnabled}}"/&gt;
      猜你喜欢
      • 1970-01-01
      • 2011-10-09
      • 2017-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-17
      • 1970-01-01
      相关资源
      最近更新 更多