DataGridRow背景色的WPF样式触发器被AlternatingRowBackground笔刷覆盖
我有一个WPF数据网格,它有一个交替的背景画笔。它被配置为每隔一行着色一次。我想在鼠标上做一些突出显示当前行的操作。但是,样式触发器似乎正在丢失给AlternatingRowBackground笔刷。我在鼠标上获得所需的行颜色。。。但仅在未使用AlternatingRowBackground笔刷绘制的行上 以下是Windows中的样式。参考资料:DataGridRow背景色的WPF样式触发器被AlternatingRowBackground笔刷覆盖,wpf,wpfdatagrid,Wpf,Wpfdatagrid,我有一个WPF数据网格,它有一个交替的背景画笔。它被配置为每隔一行着色一次。我想在鼠标上做一些突出显示当前行的操作。但是,样式触发器似乎正在丢失给AlternatingRowBackground笔刷。我在鼠标上获得所需的行颜色。。。但仅在未使用AlternatingRowBackground笔刷绘制的行上 以下是Windows中的样式。参考资料: <Window.Resources> <ResourceDictionary> <
<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>
这是数据网格:
<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>
有没有办法宣布绝对胜利者?这是一个等级问题吗?在我看来,AlternatingRowBackground笔刷胜出是因为它与声明的最具体部分直接相关
更新:根据@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>
和数据网格(减去AlternatingRowBackground笔刷):
在过去,我使用这种东西的方法是在触发器之外使用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>
并删除
DataGrid
本身的属性绑定。虽然我通常使用数据触发器来实现这一点,但通常不使用动态资源绑定。但是仍然值得一试有两种方法可以做到这一点,但都不是特别明显。由于DataGridRow(在代码中)将后台属性从父DataGrid传输到行中的本地值,正如您所注意到的,它将优先于触发器设置的值
第一种(也是最简单的)方法是不使用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>
第二个选项是使用VisualStateManager。这使您能够更好地控制不同的视觉状态,但更为冗长。幸运的是,使用Blend复制默认控件模板非常容易。除了鼠标悬停状态下的故事板,下面的大部分内容都没有改变,我在ScrollingGrid上设置了背景
抱歉,我刚才说了,有点冗长
<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>
你肯定让我朝着正确的方向走。我按照您的建议添加了Setter属性。。。不是作为{x:Type DataGridRow}的样式,而是作为{x:Type DataGrid}的单独样式。这就是诀窍。谢谢你!不确定为什么在DataGrid样式中而不是在DataGrid中定义AlternatingRowBackground。AlternatingRowBackground直接允许触发器工作,但我很高兴它能工作!谢谢你,乔希。我真的很喜欢你描述的第一个选择。我希望我可以分割接受的答案。LOL其实并不重要,但接受的答案至少应该编译吗?:)哈哈!也许是这样。。。但如果从一个拥有16个声誉的人那里夺走一个答案,而不是从你的17.3k中夺走一个答案,我会觉得很残忍……)哈哈,说吧。更好的答案应该是公认的答案。
<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>