Datagrid单元格字体颜色正在更改整行 [英] Datagrid cell font colour is changing the entire row

查看:59
本文介绍了Datagrid单元格字体颜色正在更改整行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WPF应用程序中,我有一个数据网格.我试图根据某些属性值更改数据网格中单元格的颜色.这部分正在工作.但是问题是整个行的字体颜色都已更改,我只希望一个单元格的字体在满足条件的情况下就可以更改颜色.

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.

下面是我的代码.我认为通过将TargetType用作DatagridCell,它只会影响一个单元格,而不影响整个行.

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属性:

You should probably change the SelectionUnit property in your datagrid:

SelectionUnit="Cell"

这篇关于Datagrid单元格字体颜色正在更改整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆