4

我有一个 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>

ObjectTypeConverter 检查元素是否属于给定类;和转换LightColorDarkColor根据Status属性的值生成选定的背景值。

这段代码的问题是我使用的绑定似乎总是产生一个AlternationIndex值'0',即 Converter 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. 但是,由于使用触发器工作正常,我不确定为什么 DataTrigger 不能,或者需要什么才能使其工作。

4

1 回答 1

5

您的第一个条件MultiDataTrigger找到最新的ContentPresenter,并尝试绑定到ContentPresenter.ItemsControl.AlternationIndex,并且ItemsControl.AlternationIndex不是 的有效属性ContentPresenter

尝试将其更改为,RelativeSource={RelativeSource Self}以便您将绑定到ItemsControl.AlternationIndex当前对象的

于 2012-11-23T16:49:21.697 回答