Datagrid cell font colour is changing the entire row

一曲冷凌霜 提交于 2021-02-11 16:50:43

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!