如何将单元格样式应用于DataGrid单元格

我有以下DataGrid

        

我有以下单元格样式来更改选定的Backcolor

           

我已尝试应用CellStyle="{StaticResource DataGridCell}"如上所示,并使用DynamicResource但资源无法解析。 我已经导入了正确的资源字典,因为其他样式正在工作我在这里做错了什么?

谢谢你的时间。

由于您的Style没有Key您不必在DataGrid上设置CellStyle ,默认情况下它将应用于所有DataGridCell

如果您不希望它应用于所有DataGridCell默认情况下将样式赋予x:Key并在DataGrid上设置CellStyle

例:

   

要将样式仅应用于某些DataGridRow:

创建DataGridCell样式:

 < !-- DataGridCell Style--> < Style x:Key="MyDataGridCellStyle" TargetType="{x:Type DataGridCell}">          

在您想要的列中使用它

 < !-- DataGrid -->