我正在尝试在“DataGrid”中设置单个单元格的背景颜色,但每次将颜色设置为 DataGridCell 时,都会覆盖整行的背景。我的代码:
<Setter Property="CellStyle">
<Setter.Value>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="{Binding id, Converter={StaticResource TheConverter}}" />
</Style>
</Setter.Value>
</Setter>
public class CellHighlighterConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value.ToString() == "1") return new SolidColorBrush(Colors.Red);
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
所以问题是……ID为“1”的整行都是红色的!不只是 id 单元格。这样做的正确方法是什么?