C# 更改DataGrid(WPF)行中的单元格就是更改下面行中的单元格

C# 更改DataGrid(WPF)行中的单元格就是更改下面行中的单元格,c#,wpf,datagrid,background,C#,Wpf,Datagrid,Background,我试图使用以下代码更改WPFDataGrid中包含单元格的一些错误数据的背景: DataGridRow gridRow = dgInventory.ItemContainerGenerator.ContainerFromIndex(0) as DataGridRow; DataGridCell cell = dgInventory.Columns[1].GetCellContent(gridRow).Parent as DataGridCell; cell.Background = Brush

我试图使用以下代码更改WPF
DataGrid
中包含单元格的一些错误数据的
背景

DataGridRow gridRow = dgInventory.ItemContainerGenerator.ContainerFromIndex(0) as DataGridRow;
DataGridCell cell = dgInventory.Columns[1].GetCellContent(gridRow).Parent as DataGridCell;

cell.Background = Brushes.Gray;

gridRow.IsSelected = true;
gridRow.Focus();
但是,在执行此操作时,当我向下滚动
数据网格时,同一列中的单元格在每14(大约)行之后周期性地发生上述背景颜色变化。它仅用于修改单行的
背景。有人能解决这个问题吗?提前谢谢

尝试使用以下方法:

<DataGrid Name="SimpleDataGrid" ScrollViewer.CanContentScroll="False" ... />

以物理单位表示的卷轴。默认情况下,它是启用的

有关更多信息,请参阅