从DataGrid中选择DataGridCell

13

我有一个 WPF 控件 DataGrid,我想要获取一个特定的 DataGridCell。我知道它的行和列索引,那么我该如何做呢?

我需要获取这个 DataGridCell 是因为我要访问它的内容。所以如果我有(例如)一个 DataGridTextColumn 列,我的内容将是一个 TextBlock 对象。


你好,我在方法中实现了 <DataGrid SelectedCellsChanged="DataGrid_SelectedCellsChanged" />,你可以访问所选单元格的所有属性。 - Fernando Bernal
3个回答

5
您可以使用类似下面的代码来选择单元格:
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.

1
但是Rows不是DataGrid的成员。 - gliderkite
你没错。改变了答案,昨晚可能太累了 :-) - Phil
1
好的,这种方式是可行的(如果SelectionUnit不是行),但我需要DataGridCell而不是DataGridCellInfo,因为我需要访问DataGridCell内容。我尝试了这个方法,但GetVisualChild方法返回一个空子对象,因此无法工作。 - gliderkite
1
@Phil,请看一下这个链接:http://stackoverflow.com/questions/19889780/wpf-datagrid-unable-to-select-cells-programmatically - eran otzap

2
您可以简单地使用此扩展方法 -
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 行和单元格


1
这是我使用的代码:

    /// <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处理程序)。

1
这篇文章帮了我,正如Phil在上面的回答中所描述的那样,常规方法由于我在这篇文章中所描述的奇怪原因而无法工作:http://stackoverflow.com/questions/19889780/wpf-datagrid-unable-to-select-cells-programmatically - eran otzap
1
FindVisualChild 方法的实现在哪里? - NathanAldenSr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接