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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14876223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:31:50  来源:igfitidea点击:

WPF DataGrid RowStyle for selected row not changing the background and foreground color

wpfdatagridbackground-color

提问by user2071895

I am using Visual Studio 2012 on windows 7. I need to know why the following style for Grid's selected row does not work for background and foreground colors but works perfectly fine for other properties like BorderBrush and BorderThickness etc? Though I can see them changing while mouse over grid rows.

我在 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>

Here is how I am using on grid.

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

<DataGrid RowStyle="{StaticResource gridRowStyle}">

I am stressing on to know "why" rather than solution to the problem as I already have solution to problem if I use grid cell style instead of rowstyle like following:

我强调知道“为什么”而不是问题的解决方案,因为如果我使用网格单元格样式而不是如下所示的行样式,我已经有了问题的解决方案:

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

回答by Ponraja

In the Default style of the DataGridCell having the following default style trigger.

在具有以下默认样式触发器的 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>

So if you written trigger for DataGridRow, then it will only apply to the element that was placed before the DataGridCell in visual tree.

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

So to change the Background & Foreground while selection, you must write the trigger in DataGridCell style or remove the default trigger from the style.

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

回答by nezac

Simply remove this attributs at row level in the datagrid, they have priority over the trigged properties.

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

nezac

内扎克