如何通过MultiDataTrigger在WPF DataGrid上具有选定行的可选背景色
我想在WPF如何通过MultiDataTrigger在WPF DataGrid上具有选定行的可选背景色,wpf,datagrid,multidatatrigger,Wpf,Datagrid,Multidatatrigger,我想在WPFDataGrid中以不同的背景色(灰色)显示奇数与偶数选中的行。我已经使用RowBackground=“#DDD”、AlternatingRowBackground=“#EEE”和AlternationCount=2属性与DataGrid本身一起替换未选定行的颜色 我已经浏览了StackOverflow上的许多线程,但是找不到为什么样式.Triggers没有效果。我使用它来确定它是奇数行还是偶数行 <DataGrid.RowStyle> <Style Tar
DataGrid
中以不同的背景色(灰色)显示奇数与偶数选中的行。我已经使用RowBackground=“#DDD”
、AlternatingRowBackground=“#EEE”
和AlternationCount=2
属性与DataGrid
本身一起替换未选定行的颜色
我已经浏览了StackOverflow上的许多线程,但是找不到为什么样式.Triggers
没有效果。我使用它来确定它是奇数行还是偶数行
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<!-- Changes the row style template to have more advanced "borders" (dotted, dashed, etc.) based on value converters -->
<Setter Property="Template">
<!-- ... -->
</Setter>
<!-- Changes the background color of odd rows (will do the same for even one with a slightly different color) -->
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}"
Value="True"/>
<Condition Binding="{Binding (behaviors:DataGridBehavior.IsOddRow), RelativeSource={RelativeSource Self}}"
Value="True"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Background"
Value="LightYellow"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
<!-- Same principle for even rows -->
<!-- ... -->
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
如果在mutlidata触发器
中仅使用一个条件
,则效果相同(无)
感谢您的帮助:-)您不必为此使用自定义行为,您可以使用内置的交互机制
<DataGrid AlternationCount="2"
...>
<!-- ...other properties -->
</DataGrid>
由于DataGridCell
是DataGridRow
的子项,因此在其中定义的选择颜色将遮挡行颜色。为了避免为DataGridCell
创建相同的样式,我们将其背景设置为选中时透明
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}"
Value="True">
<Setter Property="Background"
Value="Transparent"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
我最终遵循了基于…
的第一个示例,并使用了默认的附加属性ItemsControl.alternationandex
,效果很好。仍然需要考虑…
感谢您的解释。是的,使用最简单、最直接的解决方案要好得多。我将您的代码添加到我的代码中,但它会导致一个错误(我认为调试起来并不困难,但稍后会这样做)。我还对鼠标悬停在行上时用另一种不同的颜色给行着色感兴趣。我毫不怀疑,你的例子提供了一种更容易实现的方法:-)
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}"
Value="True">
<Setter Property="Background"
Value="Transparent"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>