在WPF数据网格的代码背后,如何从我的dataGrid.SelectedItem(在代码中)中获取currentCell?
非常感谢,
从帖子中试试这个
您可以从中检索行dataGrid.SelectedIndex和列dataGrid.CurrentColumn.DisplayIndex
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;
}
Run Code Online (Sandbox Code Playgroud)
编辑
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;
}
Run Code Online (Sandbox Code Playgroud)
你可以在这里找到完整的源代码(在页面末尾查找代码)
| 归档时间: |
|
| 查看次数: |
11075 次 |
| 最近记录: |