C# WPF DataGridTextColumn如何基于单元格内容在样式中绑定前景色
我有一个DataGrid,其中有以下内容C# WPF DataGridTextColumn如何基于单元格内容在样式中绑定前景色,c#,wpf,wpf-controls,wpfdatagrid,C#,Wpf,Wpf Controls,Wpfdatagrid,我有一个DataGrid,其中有以下内容 <DataGridTextColumn Header="MyHeader" HeaderStyle="{StaticResource ServiceStatusColumn}" ElementStyle="{StaticResource ServiceStatusElementStyle}" Binding="{B
<DataGridTextColumn Header="MyHeader"
HeaderStyle="{StaticResource ServiceStatusColumn}"
ElementStyle="{StaticResource ServiceStatusElementStyle}"
Binding="{Binding PuServiceStatus, Converter={StaticResource serviceStatusText}}">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell" BasedOn="{StaticResource ServiceStatusCell}">
<Setter Property="Foreground" Value="{Binding PuServiceStatus, Converter={StaticResource serviceStatusColor}}" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
任何帮助都将不胜感激。将setter放在DataGridRow上对我很有用。但是,这会更改整行的前景
<Style x:Key="RowProcessIdAndLevel" TargetType="{x:Type DataGridRow}" BasedOn="{StaticResource {x:Type DataGridRow}}">
<Setter Property="Foreground" Value="{Binding Level, Converter={StaticResource LevelToForeground}}"/>
</Style>
您是否看到了
PuServiceStatus
的正确文本,只是前景错误?如果是,则可能是serviceStatusColor
转换器有问题。检查转换器是否返回正确的笔刷值。
<Style x:Key="ServiceStatusCell" TargetType="DataGridCell">
<Setter Property="TextBlock.TextAlignment" Value="Center" />
<Setter Property="Width" Value="20"/>
<Setter Property="Height" Value="20"/>
</Style>
<Style x:Key="RowProcessIdAndLevel" TargetType="{x:Type DataGridRow}" BasedOn="{StaticResource {x:Type DataGridRow}}">
<Setter Property="Foreground" Value="{Binding Level, Converter={StaticResource LevelToForeground}}"/>
</Style>