您无法通过这种方式访问DataGridCell.Content,请使用DataTrigger,而不是基于您的DataGrid.SelectedItem.YourProperty,如下所示:
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="FontWeight" Value="Bold" />
<Style.Triggers>
<DataTrigger Binding="{Binding YourProperty}" Value="0">
<Setter Property="FontWeight" Value="Normal"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
编辑:
假设您的DataGridColumns 是基于文本的,那么您可以使用IValueConverter,如下所示:
请注意,如果某些数据网格列不是基于文本的,则此解决方案仍适用于那些基于文本的列。
Xaml:
<Window.Resources>
<local:FontWeightConverter x:Key="fontWeightConverter"/>
</Window.Resources>
...
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Setters>
<Setter Property="FontWeight"
Value="{Binding RelativeSource={RelativeSource Self},
Path=Content.Text,
Converter={StaticResource fontWeightConverter}}" />
</Style.Setters>
</Style>
</DataGrid.CellStyle>
转换器:
public class FontWeightConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
if (value != null && value.ToString() == "0")
return FontWeights.Normal;
return FontWeights.Bold;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}