In my WPF app I have a datagrid. I am trying to change the colour of a cell in a datagrid based on some property value. This part is working. However the issue is the entire row has its font colour changed, I just want the one cell's font to change colour if the condition is meet.
Below is my code. I thought by putting the TargetType as a DatagridCell that it would only effect a cell not the entire row.
<!-- DataGrid Cell style -->
<Style x:Key="DG_Cell" TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border x:Name="border"
Background="Transparent"
BorderBrush="Transparent"
BorderThickness="1"
SnapsToDevicePixels="True">
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Border>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding NominalDiff, Converter={StaticResource nominalPosToBool}, ConverterParameter=0}" Value="True">
<Setter Property="Foreground" Value="Green"/>
</DataTrigger>
<DataTrigger Binding="{Binding NominalDiff, Converter={StaticResource nominalNegToBool}, ConverterParameter=0}" Value="True">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding PriceDiff, Converter={StaticResource priceToBool}, ConverterParameter=0}" Value="True">
<Setter Property="Foreground" Value="Blue"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontWeight" Value="Bold"/>
</Trigger>
</Style.Triggers>
</Style>
您可能应该在数据网格中更改SelectionUnit属性:
SelectionUnit="Cell"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.