Master_T
Master_T

Reputation: 8007

WPF DataGrid: changing cell foreground color with triggers

I have a WPF DataGrid that has some columns that need to have Blue text in them. This is by specification and cannot be changed.

Now, this would not be a problem normally, however when the user selects a row in the grid the row gets highlighted in blue, as is usual in all versions of windows.

This is problematic, because the row becomes hard to read:

enter image description here

(note that the image above is from Win10 where the blue of the selection is quite light, but on the client's machines, which are Windows Server 2008, the "selection blue" is much darker and the text is basically unreadable)

So I tried to apply the following style to the cells of that particular column:

<Style TargetType="DataGridCell" x:Key="styleBlueCell">
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="False">
            <Setter Property="Foreground" Value="Blue"/>
        </Trigger>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Foreground" Value="White"/>
        </Trigger>
    </Style.Triggers>
</Style>

This works almost perfectly... but for one scenario: when a row is selcted but the DataGrid itself does not have the focus, the row turns Gray, which messes things up:

enter image description here

I tried to mess around with MultiTriggers, like this:

<Style TargetType="DataGridCell" x:Key="styleBlueCell2">
            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding ElementName=gridAvanzamenti, Path=IsFocused}" Value="False"/>
                        <Condition Binding="{Binding IsSelected}" Value="True" />
                    </MultiDataTrigger.Conditions>
                    <Setter Property="Foreground" Value="Blue"/>
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>

but no matter what I try (binding by element name, find ancestor, etc.) I can't seem to be able to Bind to the parent DataGrid. Does anyone know how to do it?

Upvotes: 1

Views: 2103

Answers (1)

Master_T
Master_T

Reputation: 8007

After more trial and error and a useful tip from Ed Plunkett, heeres the full solution:

<Style TargetType="DataGridCell" x:Key="styleBlueCell">
    <Style.Setters>
        <Setter Property="Foreground" Value="Blue"/>
    </Style.Setters>
    <Style.Triggers>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=IsKeyboardFocusWithin}" Value="True"/>
                <Condition Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}, Path=IsSelected}" Value="True"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Foreground" Value="White"/>
        </MultiDataTrigger>
    </Style.Triggers>
</Style>

Upvotes: 2

Related Questions