问题
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>
回答1:
You should probably change the SelectionUnit property in your datagrid:
SelectionUnit="Cell"
来源:https://stackoverflow.com/questions/23386607/datagrid-cell-font-colour-is-changing-the-entire-row