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

时间:2013-02-14 13:45:48

标签: 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>

以下是我在网格上的使用方法。

<DataGrid RowStyle="{StaticResource gridRowStyle}">

我强调要知道“为什么”而不是解决问题,因为如果我使用网格单元样式而不是像下面的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>

2 个答案:

答案 0 :(得分: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>

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

所以改变背景&amp;选择前景时,必须以DataGridCell样式编写触发器或从样式中删除默认触发器。

答案 1 :(得分:0)

只需在数据网格中的行级别删除此属性,它们优先于触发属性。

nezac

相关问题