c# – Datagrid:如何获取SelectedItem的CurrentCell?

WPF数据网格的代码背后,如何从我的dataGrid.SelectedItem(在代码中)中获取currentCell?

非常感谢,

解决方法:

post开始尝试

您可以通过dataGrid.CurrentColumn.DisplayIndex从dataGrid.SelectedIndex和column中检索行.

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

                // try to get the cell but it may possibly be virtualized
                DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                if (cell == null)
                {
                    // now try to bring into view and retreive the cell
                    dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]);

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

                return cell;
            }

            return null;
}

编辑

public static DataGridRow GetRow(DataGrid dataGrid, int index)
    {
    DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
        if (row == null)
        {

            dataGrid.ScrollIntoView(dataGrid.Items[index]);
            dataGrid.UpdateLayout();

            row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
        }

        return row;
    }

你可以找到完整的源代码here(在页面末尾查找代码)

上一篇:WPF DataGrid 使用CellTemplateSelector 时SelectTemplate方法Item参数为NULL


下一篇:c# – 从DataGrid中删除重复行