handles
handles

Reputation: 7853

Setting the text Colour of a WPF DataGrid Row when row is selected

I'm trying to change the colour of Text in the selected row in a WPF datagrid. By default it changes the text colour white is there a way to change this using styles / triggers etc?

Thanks in advance!

Upvotes: 19

Views: 23372

Answers (2)

Ian
Ian

Reputation: 861

If you want to completely remove the Foreground color changes (say, if your DataGrid has different colors for different rows), you can do this:

    <Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Self}, Path=Foreground}" />
            </Trigger>
        </Style.Triggers>
    </Style>

If you want to give this style a name, like in the previous answer, add x:Key.

Upvotes: 4

Fredrik Hedblad
Fredrik Hedblad

Reputation: 84657

Try this

<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}" >
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Foreground" Value="Green"/>
        </Trigger>
    </Style.Triggers>
</Style>

Then you can use it in the columns that you see fit like

<DataGrid ...>
    <DataGrid.Columns>
        <DataGridTextColumn CellStyle="{StaticResource DataGridCellStyle}" .../>

If you want it to apply to all columns you can change the x:key of the Style to

<Style x:Key="{x:Type DataGridCell}" TargetType="{x:Type DataGridCell}" >

Upvotes: 33

Related Questions