所选行的WPF DataGrid RowStyle不更改背景和前景色

use*_*895 6 wpf datagrid background-color

我在Windows 7上使用Visual Studio 2012.我需要知道为什么Grid的选定行的以下样式不适用于背景和前景色,但对于BorderBrush和BorderThickness等其他属性的效果非常好?虽然我可以看到它们在鼠标悬停在网格行上时发生变化

<Style x:Key="gridRowStyle" TargetType="{x:Type DataGridRow}">
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="PeachPuff"/>
            <Setter Property="Foreground" Value="BlueViolet"/>
        </Trigger>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="PeachPuff"/>
            <Setter Property="Foreground" Value="BlueViolet"/>
            <Setter Property="BorderBrush" Value="BlueViolet" />
            <Setter Property="BorderThickness" Value="2" />

        </Trigger>
    </Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)

这是我在网格上使用的方式.

<DataGrid RowStyle="{StaticResource gridRowStyle}">
Run Code Online (Sandbox Code Playgroud)

我强调要知道"为什么"而不是解决问题,因为我已经有问题的解决方案,如果我使用网格单元格样式而不是像下面的rowstyle:

<Style x:Key="gridCellStyle" TargetType="{x:Type DataGridCell}">
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="PeachPuff"/>
            <Setter Property="Foreground" Value="BlueViolet"/>
        </Trigger>
    </Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)

Pon*_*aja 0

在具有以下默认样式触发器的 DataGridCell 的默认样式中。

<Trigger Property="IsSelected" Value="True">
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
    <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
Run Code Online (Sandbox Code Playgroud)

因此,如果您为 DataGridRow 编写了触发器,那么它将仅适用于可视化树中放置在 DataGridCell 之前的元素。

因此,要在选择时更改背景和前景,您必须以 DataGridCell 样式编写触发器或从样式中删除默认触发器。