Datagrid:如何获取SelectedItem的CurrentCell?

在WPF datagrid的代码背后,如何从我的dataGrid.SelectedItem(In Code)中获取currentCell?

非常感谢,

从post中试试这个

您可以通过dataGrid.CurrentColumn.DisplayIndexdataGrid.SelectedIndex和column中检索行。

 public static DataGridCell GetCell(DataGrid dataGrid, int row, int column) { DataGridRow rowContainer = GetRow(dataGrid, row); if (rowContainer != null) { DataGridCellsPresenter presenter = GetVisualChild(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; } 

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

您可以在DataGrid本身中使用CurrentCell.Item属性:

 DataGridCell cell = (DataGridCell)myDataGrid.CurrentCell.Item;