AlternationIndex的MultiDataTrigger

5

我有一个被声明为以下方式的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属性的值产生所选的背景值。

这段代码的问题在于我使用的绑定似乎总是生成一个'0'的AlternationIndex值,即每个条目都使用了LightColor转换器。除了上述代码,我还尝试了相同结果的以下绑定:

<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中单独定义的。但是,由于使用触发器可以正常工作,我不确定为什么数据触发器不起作用,或者需要什么才能使其工作。

1个回答

6
你的 MultiDataTrigger 中的第一个条件查找到最近的 ContentPresenter,并尝试绑定到 ContentPresenter.ItemsControl.AlternationIndex,但是 ItemsControl.AlternationIndex 不是 ContentPresenter 的有效属性。
请将其更改为 RelativeSource={RelativeSource Self},这样你就可以绑定到当前对象的 ItemsControl.AlternationIndex

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接