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

后端 未结 2 1308
青春惊慌失措
青春惊慌失措 2020-12-16 13:04

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 / trig

相关标签:
2条回答
  • 2020-12-16 13:41

    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}" >
    
    0 讨论(0)
  • 2020-12-16 13:53

    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.

    0 讨论(0)
提交回复
热议问题