简体   繁体   中英

How to: Change the DataGrid IsSelected Background (C#, WPF)

How to: Change the DataGrid IsSelected Background (C#, WPF)

I do have the following code in my App.xaml :

<Application.Resources>
    <Style TargetType="{x:Type DataGridRow}">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Orange" />
            </Trigger>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="WhiteSmoke" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Application.Resources>

Unfortunately, the following result is what I get: 在此处输入图片说明

What do I need to do to fix my Problem?

You can add this bit of Xaml to your data grid resources...

                <DataGrid.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Yellow"/>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Red"/>
                </DataGrid.Resources>

The two brushes work together to set the background/foreground when a row is selected.

To set the default colours for a DataGrid row, you can add this snippet...

                <DataGrid.RowStyle>
                    <Style TargetType="DataGridRow">
                        <Setter Property="Background" Value="Pink" />
                        <Setter Property="Foreground" Value="DodgerBlue"/>
                    </Style>
                </DataGrid.RowStyle>

This sets the background to Pink and the Foreground to DodgerBlue.

More info on the SystemColors static resource is at http://blogs.msdn.com/b/wpf/archive/2010/11/30/systemcolors-reference.aspx

您必须设置DataGridCellIsSelected属性的样式,因为它(显然)是在DataGridRow之后呈现的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM