WPF DataGrid AlternatingRowBackground覆盖背景的Style DataTrigger [英] WPF DataGrid AlternatingRowBackground overriding the Style DataTrigger for background
问题描述
我有一个DataGrid,其中显示了多行数据.为了帮助视觉上区分行,我为交替的行添加了背景色.
I have a DataGrid where I show many lines of data. To help visually differentiate the rows I've added a background colour to alternating rows.
但是,有些行包含非常有趣的数据,我希望吸引用户的注意,因此我使用Style DataTrigger突出显示了这些特定行.
But, there are some rows that contains very interesting data that I want to attract the user's attention to, and so I use a Style DataTrigger to highlight those specific rows.
我的问题是交替的背景颜色优先-只有奇数行(没有背景颜色)显示突出显示.
My problem is that the alternating background colour takes precedence - only the odd rows (no background colour) show the highlight.
请注意,这是一个使用MVVM模式的数据绑定DataGrid(无"code-behind").
Note, this is a data-bound DataGrid using the MVVM pattern (no "code-behind").
(非常删减)代码如下:
The (very cutdown) code is as follows:
<DataGrid ItemsSource="{Binding FilteredTraceMessages, Mode=OneWay}"
AlternatingRowBackground="AliceBlue"
.......>
<DataGrid.Columns>
....
</DataGrid.Columns>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Severity}" Value="Error">
<Setter Property="Background" Value="LightSalmon"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Severity}" Value="Warning">
<Setter Property="Background" Value="LemonChiffon"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</DataGrid>
推荐答案
您必须将 Backround
设置为相同的优先级.请参见依赖项属性设置优先级列表
从 DataGrid
中删除 AlternatingRowBackground ="AliceBlue"
,然后在其中放置 AlternationCount ="2"
.然后首先添加 AlternationIndex
的触发器.
You have to set the Backround
on the same precedence level. See Dependency Property Setting Precedence List
Delete AlternatingRowBackground="AliceBlue"
from the DataGrid
and put AlternationCount="2"
there. Then add at the first place a trigger for AlternationIndex
.
<DataGrid ItemsSource="{Binding FilteredTraceMessages, Mode=OneWay}" AlternationCount="2"
.......>
<DataGrid.Columns>
....
</DataGrid.Columns>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<Trigger Property="AlternationIndex" Value="1">
<Setter Property="Background" Value="AliceBlue"/>
</Trigger>
<DataTrigger Binding="{Binding Severity}" Value="Error">
<Setter Property="Background" Value="LightSalmon"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Severity}" Value="Warning">
<Setter Property="Background" Value="LemonChiffon"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</DataGrid>
这篇关于WPF DataGrid AlternatingRowBackground覆盖背景的Style DataTrigger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!