我有一个 ListView 声明为:
<ListView x:Name="Tree"
ItemsSource="{Binding ElementName=This, Path=Some.Path.Values}"
AlternationCount="2"
ScrollViewer.CanContentScroll="False">
和定义为的样式
<UserControl.Resources>
<Style TargetType="ListViewItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="SteelBlue"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Gray"/>
</Style.Resources>
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="White" />
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="GhostWhite" />
</Trigger>
</Style.Triggers>
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Top" />
<EventSetter Event="Loaded" Handler="ContinueLoading" />
</Style>
这种组合产生了最初的期望行为,即交替背景高光的行为。新的期望行为是根据给定 ListView 项的属性值更改背景颜色;因此
Style.Triggers
改为<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="White" />
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="GhostWhite" />
</Trigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}}, Path=(ItemsControl.AlternationIndex)}" Value="0"/>
<Condition Binding="{Binding Converter={x:Static controls:Converters.ObjectType}}" Value="{x:Type client:DocumentEntryTypeA}" />
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="{Binding Converter={x:Static controls:Converters.LightColor}, UpdateSourceTrigger=PropertyChanged, Path=Status}" />
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}}, Path=(ItemsControl.AlternationIndex)}" Value="1"/>
<Condition Binding="{Binding Converter={x:Static controls:Converters.ObjectType}}" Value="{x:Type client:DocumentEntryTypeA}" />
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="{Binding Converter={x:Static controls:Converters.DarkColor}, UpdateSourceTrigger=PropertyChanged, Path=Status}" />
</MultiDataTrigger>
</Style.Triggers>
</UserControl.Resources>
ObjectType
转换器检查元素是否属于给定类; LightColor
和 DarkColor
转换器根据 Status
的值生成选定的背景值。属性(property)。这段代码的问题是我使用的绑定(bind)似乎总是产生
AlternationIndex
'0' 的值,即 Converter LightColor 用于每个条目。除了上面的代码,我还尝试了以下绑定(bind),结果相同:<Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}, Path=AlternationIndex}" Value="0"/>
<Condition Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListView}}, Path=(ItemsControl.AlternationIndex)}" Value="0"/>
根据我见过的示例,大多数解决方案都没有将样式与对象分开;在我的情况下,样式在
UserControl.Resources
中单独定义.但是,由于使用触发器工作正常,我不确定为什么 DataTrigger 不能,或者需要什么才能使其工作。
最佳答案
MultiDataTrigger
中的第一个条件查找最新的 ContentPresenter
, 并尝试绑定(bind)到 ContentPresenter.ItemsControl.AlternationIndex
, 和 ItemsControl.AlternationIndex
不是 ContentPresenter
的有效属性.
尝试将其更改为 RelativeSource={RelativeSource Self}
所以你将绑定(bind)到 ItemsControl.AlternationIndex
当前对象的
关于wpf - MultiDataTrigger 与 AlternationIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13532638/