wpf - MultiDataTrigger 与 AlternationIndex

标签 wpf xaml

我有一个 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转换器检查元素是否属于给定类; LightColorDarkColor转换器根据 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/

相关文章:

c# - 添加/启用装饰器后如何刷新/更新wpf窗口

C# WPF - DataGridComboBoxColumn ItemsSource

xaml - UWP ToggleSwitch 将内容放在左侧

c# - 如何将项目添加到 ObservableCollection?

c# - “有效载荷文件不存在”是什么意思?

wpf - 根据 Visual Studio 主题的变化更改图标

c# - 功能区更改标题文本颜色

c# - WPF绑定(bind)(使用触发器)不能从文本框工作到标签

c# - 如何替换 richtextbox 中插入符号位置下的单词? - 实现拼写检查建议

c# - 如何为 WPF TreeView 设置垂直滚动条,而不是水平滚动条?