我想标记数据网格的某些单元格以更改标记单元格的颜色。我可以用他们的代码为单个单元格做到这一点:
public static DataGridRow GetRow(this DataGrid dataGrid, int index)
{
DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
dataGrid.UpdateLayout();
dataGrid.ScrollIntoView(dataGrid.Items[index]);
row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
}
return row;
}
public static int GetRowIdx(this DataGrid dataGrid, DataGridCellInfo cellInfo)
{
// Use reflection to get DataGridCell.RowDataItem property value.
DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(cellInfo.Item);
if (row == null)
throw new NullReferenceException("Fehler: Keine Index gefunden da DataGridRow null!");
return row.GetIndex();
}
public static DataGridCell GetCurrentCell(this DataGrid dataGrid)
{
int row = GetRowIdx(dataGrid, dataGrid.CurrentCell);
int column = dataGrid.CurrentColumn.DisplayIndex;
return GetCell(dataGrid, row, column);
}
呼唤:
DataGridCell currentCell = DataGridExtension.GetCurrentCell(dataGrid1);
currentCell.Background = Brushes.LightGray;
有人知道如何更改此代码,以便我可以标记例如 5 个单元格并更改它们的颜色?