用于 DataGridRow 背景颜色的 WPF 样式触发器被 AlternatingRowBackground Brush 改变
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3614837/
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
WPF Style Trigger for DataGridRow Background Color Trumped by AlternatingRowBackground Brush
提问by Mike L
I have a WPF DataGrid which has an AlternatingRowBackground brush. It's configured to color every other row. I'd like to do something on mouse over that highlights the current row. However, the Style Trigger seems to be losing out to the AlternatingRowBackground brush. I get the desired row coloration on the mouse over... but only on the rows that are not painted with the AlternatingRowBackground brush.
我有一个 WPF DataGrid,它有一个 AlternatingRowBackground 画笔。它被配置为每隔一行着色。我想在鼠标悬停时做一些突出显示当前行的事情。但是,样式触发器似乎输给了 AlternatingRowBackground 画笔。我在鼠标悬停时获得所需的行着色...但仅在未使用 AlternatingRowBackground 画笔绘制的行上。
Here is the Style in Windows.Resources:
这是 Windows.Resources 中的样式:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Skins/MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="Background"
Value="Red" />
<Setter Property="FontWeight"
Value="ExtraBold" />
<Setter Property="Height"
Value="20" />
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
</Window.Resources>
And here is the DataGrid:
这是 DataGrid:
<DataGrid Margin="25,15,25,0"
VerticalAlignment="Top"
ItemsSource="{Binding DocumentTypeList}"
AutoGenerateColumns="False"
Height="500"
AlternationCount="2"
FrozenColumnCount="2"
AlternatingRowBackground="{DynamicResource AlternatingRow}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Abbreviation}"
Header="Abbreviation" />
<DataGridTextColumn Binding="{Binding Title}"
Header="Title" />
<DataGridTextColumn Binding="{Binding Fee}"
Header="Fee" />
<DataGridTextColumn Binding="{Binding SpecialInstructions}"
Header="Special Instructions" />
</DataGrid.Columns>
</DataGrid>
Is there a way to declare the absolute winner? Is the issue one of a hierarchy? It seems to me that the AlternatingRowBackground brush wins out because it is directly associated with the most specific part of the declaration.
有没有办法宣布绝对赢家?问题是层次结构之一吗?在我看来,AlternatingRowBackground 画笔胜出,因为它与声明中最具体的部分直接相关。
Update:Here is the correct syntax, based upon @Val's guidance:
更新:以下是正确的语法,基于@Val 的指导:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Skins/MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="Background"
Value="Red" />
<Setter Property="FontWeight"
Value="ExtraBold" />
<Setter Property="Height"
Value="20" />
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type DataGrid}">
<Setter Property="AlternatingRowBackground" Value="{DynamicResource AlternatingRow}"/>
</Style>
</ResourceDictionary>
</Window.Resources>
And the DataGrid (minus the AlternatingRowBackground brush):
和 DataGrid(减去 AlternatingRowBackground 画笔):
<DataGrid Margin="25,15,25,0"
VerticalAlignment="Top"
ItemsSource="{Binding DocumentTypeList}"
AutoGenerateColumns="False"
Height="500"
AlternationCount="2"
FrozenColumnCount="2">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Abbreviation}"
Header="Abbreviation" />
<DataGridTextColumn Binding="{Binding Title}"
Header="Title" />
<DataGridTextColumn Binding="{Binding Fee}"
Header="Fee" />
<DataGridTextColumn Binding="{Binding SpecialInstructions}"
Header="Special Instructions" />
</DataGrid.Columns>
</DataGrid>
采纳答案by Val
What's worked for me in the past with this kind of thing, is to use a setter outside the triggers eg:
过去对我有用的东西是在触发器之外使用一个 setter,例如:
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="Background"
Value="Red" />
<Setter Property="FontWeight"
Value="ExtraBold" />
<Setter Property="Height"
Value="20" />
</Trigger>
</Style.Triggers>
<Setter Property="AlternatingRowBackground"
Value="{DynamicResource AlternatingRow}"/>
</Style>
And them remove the property binding on the DataGrid
itself. Although I usually do this with data triggers, and not usually with dynamic resource bindings. But still might be worth a shot
他们删除了DataGrid
自身的属性绑定。尽管我通常使用数据触发器来执行此操作,但通常不使用动态资源绑定。但仍然值得一试
回答by Josh
There's two ways to do this, neither is particularly obvious. Since DataGridRow transfers (in code) the background property from the parent DataGrid to a local value in the row, as you noted it will take precedence over the value set by your trigger.
有两种方法可以做到这一点,都不是特别明显。由于 DataGridRow 将背景属性(在代码中)从父 DataGrid 传输到行中的本地值,正如您所指出的,它将优先于触发器设置的值。
The first (and simplest) way is to not use the AlternatingRowBackground or RowBackground but instead use triggers to alternate the background color as Val suggested. His example is not complete though and will not work as-is. The correct style and usage would be as follows. Note that you need to set the AlternationCount on DataGrid or else the rows will never get alternating indexes.
第一种(也是最简单的)方法是不使用 AlternatingRowBackground 或 RowBackground ,而是使用触发器来改变 Val 建议的背景颜色。他的例子虽然不完整,也不会按原样工作。正确的样式和用法如下。请注意,您需要在 DataGrid 上设置 AlternationCount,否则行将永远不会获得交替索引。
<DataGrid AlternationCount="2">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="White"/>
<Setter Property="FontWeight" Value="Normal"/>
<Style.Triggers>
<Trigger Property="AlternationIndex" Value="1">
<Setter Property="Background" Value="Wheat"/>
<Setter Property="FontWeight" Value="Bold"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Khaki"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</DataGrid>
The second option is to use the VisualStateManager. This gives you far more control over the different visual states but it is more verbose. Fortunately it's quite easy to copy the default control template using Blend. Most of the following is unchanged except the Storyboard in the MouseOver state and I've set a background on the selectiveScrollingGrid.
第二个选项是使用 VisualStateManager。这使您可以更好地控制不同的视觉状态,但它更加冗长。幸运的是,使用 Blend 可以很容易地复制默认控件模板。除了处于 MouseOver 状态的 Storyboard 和我在 selectionScrollingGrid 上设置了背景外,以下大部分内容都没有变化。
Sorry for the wrap, but like I said, it's a bit more verbose.
很抱歉,但就像我说的那样,它有点冗长。
<DataGrid AlternationCount="2" AlternatingRowBackground="Wheat">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DataGridRow">
<Border x:Name="DGR_Border"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
SnapsToDevicePixels="True">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="Normal_AlternatingRow"/>
<VisualState x:Name="Unfocused_Editing"/>
<VisualState x:Name="Normal_Editing"/>
<VisualState x:Name="Unfocused_Selected"/>
<VisualState x:Name="Normal_Selected"/>
<VisualState x:Name="MouseOver_Unfocused_Editing"/>
<VisualState x:Name="MouseOver_Editing"/>
<VisualState x:Name="MouseOver_Unfocused_Selected"/>
<VisualState x:Name="MouseOver_Selected"/>
<VisualState x:Name="MouseOver">
<Storyboard Storyboard.TargetName="Highlight">
<ColorAnimation Duration="0" Storyboard.TargetProperty="Color" To="Khaki"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<SelectiveScrollingGrid x:Name="selectiveScrollingGrid">
<SelectiveScrollingGrid.Background>
<SolidColorBrush x:Name="Highlight" Color="Transparent"/>
</SelectiveScrollingGrid.Background>
<SelectiveScrollingGrid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</SelectiveScrollingGrid.ColumnDefinitions>
<SelectiveScrollingGrid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</SelectiveScrollingGrid.RowDefinitions>
<DataGridCellsPresenter Grid.Column="1" ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
<DataGridDetailsPresenter Grid.Column="1" Grid.Row="1" SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{TemplateBinding DetailsVisibility}"/>
<DataGridRowHeader Grid.RowSpan="2" SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
</SelectiveScrollingGrid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowStyle>
</DataGrid>