Wpf 所选DataGridRow的背景色不工作/如何与AlternationIndex组合?
我正在尝试设计一个Wpf 所选DataGridRow的背景色不工作/如何与AlternationIndex组合?,wpf,xaml,Wpf,Xaml,我正在尝试设计一个DataGrid,这也包括设计DataGridRow。我想要的是在我的行上有一个交替的背景行为(比如,白色/灰色);选择行时,背景应为黄绿色。不知何故,下面的代码不起作用,但我确信在选择部分使用DataGridCell会起作用;我只是想知道为什么这不起作用: <Style TargetType="{x:Type DataGridRow}"> <Style.Triggers> <Trigger Property="DataGr
DataGrid
,这也包括设计DataGridRow
。我想要的是在我的行上有一个交替的背景行为(比如,白色/灰色);选择行时,背景应为黄绿色。不知何故,下面的代码不起作用,但我确信在选择部分使用DataGridCell会起作用;我只是想知道为什么这不起作用:
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="DataGridRow.AlternationIndex" Value="0">
<Setter Property="Background" Value="{StaticResource EnBWLichtgrau}"/>
</Trigger>
<Trigger Property="DataGridRow.AlternationIndex" Value="1">
<Setter Property="Background" Value="White"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="DataGridRow.AlternationIndex" Value="0"/>
<Condition Property="DataGridRow.IsSelected" Value="True"/>
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Background" Value="Pink"/>
</MultiTrigger.Setters>
</MultiTrigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type DataGrid}">
<Setter Property="AlternationCount" Value="2"/>
<Setter Property="BorderBrush" Value="{StaticResource EnBWAnthrazitgrau}"/>
<Setter Property="HeadersVisibility" Value="Column"/>
</Style>
也许有些设定器会覆盖多触发设定器,也许还有别的,我不知道。多触发器现在不是很必要,但即使是IsSelected上的一个简单触发器也不能以这种方式工作
正如我所说,我可能会去设计DataGridCell,但“为什么”是我感兴趣的:-)谢谢
EDIT:我试着用
DataGridCell
代替IsSelected
部分,它按预期工作。但是,如果我希望所选行的背景颜色根据单元格所在的DataGridRow
的替代索引而不同,该怎么办?是否有某种方法可以在DataGridCell
样式定义中获取此索引的值,例如?我对ListBoxItem也有同样的问题。似乎AlternationIndex触发器是在IsSelected触发器之后触发的,或者IsSelected完全被AlternationIndex触发器绕过。我们可以从某人那里得到关于如何绕过此问题的更新吗?