C# 从 DataGrid 中选择 DataGridCell

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9978119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 11:27:50  来源:igfitidea点击:

Select DataGridCell from DataGrid

c#.netwpfwpfdatagriddatagridcell

提问by gliderkite

I have a DataGridWPF control and I want to get a specific DataGridCell. I know the row and column indices. How can I do this?

我有一个DataGridWPF 控件,我想获得一个特定的DataGridCell. 我知道行和列索引。我怎样才能做到这一点?

I need the DataGridCellbecause I have to have access to its Content. So if I have (for example) a column of DataGridTextColum, my Content will be a TextBlockobject.

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

回答by Phil

You can use code similar to this to select a cell:

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

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

dataGrid.SelectedCells.Clear();
dataGrid.SelectedCells.Add(dataGridCellInfo);
dataGrid.CurrentCell = dataGridCellInfo;

I can't see a way to update the contents of a specific cell directly, so in order to update the content of a specific cell I would do the following

我看不到直接更新特定单元格内容的方法,因此为了更新特定单元格的内容,我将执行以下操作

// 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.

回答by akjoshi

You can simply use this extension method-

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

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

and you can get a cell of a DataGrid by an existing row and column id:

您可以通过现有的行和列 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.ScrollIntoViewis the key to make this work in case DataGrid is virtualized and required cell is not in view currently.

grid.ScrollIntoView是在 DataGrid 已虚拟化且当前未查看所需单元格的情况下进行此工作的关键。

Check this link for details - Get WPF DataGrid row and cell

检查此链接以获取详细信息 -获取 WPF DataGrid 行和单元格

回答by ouflak

Here is the code I used:

这是我使用的代码:

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

This assumes that the DataGrid has already been loaded (executed its own Loaded handler).

这假设 DataGrid 已经被加载(执行了它自己的 Loaded 处理程序)。