从DataGrid中选择DataGridCell

本文关键字:DataGridCell 选择 DataGrid | 更新日期: 2023-09-27 18:29:48

我有一个DataGrid WPF控件,我想获得一个特定的DataGridCell。我知道行和列索引。我该怎么做?

我需要DataGridCell,因为我必须访问它的内容。因此,如果我(例如)有一列DataGridTextColum,我的Content将是一个TextBlock对象。

从DataGrid中选择DataGridCell

您可以使用类似的代码来选择一个单元格:

var dataGridCellInfo = new DataGridCellInfo(
    dataGrid.Items[rowNo], dataGrid.Columns[colNo]);
dataGrid.SelectedCells.Clear();
dataGrid.SelectedCells.Add(dataGridCellInfo);
dataGrid.CurrentCell = dataGridCellInfo;

我看不到直接更新特定单元格内容的方法,所以为了更新特定单元格的内容,我会执行以下

// gets the data item bound to the row that contains the current cell
// and casts to your data type.
var item = dataGrid.CurrentItem as MyDataItem;
if(item != null){
    // update the property on your item associated with column 'n'
    item.MyProperty = "new value";
}
// assuming your data item implements INotifyPropertyChanged the cell will be updated.

您可以简单地使用这个扩展方法-

public static DataGridRow GetSelectedRow(this DataGrid grid)
{
    return (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem);
}

您可以通过现有的行和列id:获得DataGrid的一个单元格

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;
}

grid.ScrollIntoView是实现这一功能的关键,以防DataGrid被虚拟化,并且所需的单元格当前不在视图中。

查看此链接了解详细信息-获取WPF DataGrid行和单元格

这是我使用的代码:

    /// <summary>
    /// Get the cell of the datagrid.
    /// </summary>
    /// <param name="dataGrid">The data grid in question</param>
    /// <param name="cellInfo">The cell information for a row of that datagrid</param>
    /// <param name="cellIndex">The row index of the cell to find. </param>
    /// <returns>The cell or null</returns>
    private DataGridCell TryToFindGridCell(DataGrid dataGrid, DataGridCellInfo cellInfo, int cellIndex = -1)
    {
        DataGridRow row;
        DataGridCell result = null;
        if (dataGrid != null && cellInfo != null)
        {
            if (cellIndex < 0)
            {
                row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(cellInfo.Item);
            }
            else
            {
                row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(cellIndex);
            }
            if (row != null)
            {
                int columnIndex = dataGrid.Columns.IndexOf(cellInfo.Column);
                if (columnIndex > -1)
                {
                    DataGridCellsPresenter presenter = this.FindVisualChild<DataGridCellsPresenter>(row);
                    if (presenter != null)
                    {
                        result = presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex) as DataGridCell;
                    }
                    else
                    {
                        result = null;
                    }
                }
            }
        }
        return result;
    }`

这假设DataGrid已经加载(执行其自己的loaded处理程序)。