Xaml 带数据触发器的交替索引

Xaml 带数据触发器的交替索引,xaml,binding,triggers,listbox,multidatatrigger,Xaml,Binding,Triggers,Listbox,Multidatatrigger,我有一个交替索引为2的列表框。然后我设置了一个样式,为不同的交替索引提供样式 <Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBoxItem}">

我有一个交替索引为2的列表框。然后我设置了一个样式,为不同的交替索引提供样式

<Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Grid>
                    <Border x:Name="Bd" SnapsToDevicePixels="true">
                        <ContentPresenter Margin="{TemplateBinding Padding}"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                    </Border>
                    <Rectangle x:Name="HoverRectangle"
                               Height="Auto"
                               SnapsToDevicePixels="True"
                               Stroke="{StaticResource Gold}"
                               StrokeDashCap="Square"
                               StrokeThickness="0" />
                    <Rectangle x:Name="KeyboardFocusRectangle"
                               Height="Auto"
                               SnapsToDevicePixels="True"
                               Stroke="{StaticResource BrightBlue}"
                               StrokeDashCap="Square"
                               StrokeThickness="0" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                        <Setter TargetName="Bd" Property="Background" Value="{StaticResource LightGray}" />
                    </Trigger>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                        <Setter TargetName="Bd" Property="Background" Value="{StaticResource VeryLightGray}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我还想改变项目的背景,如果他们在未来发生。我有一个函数IsFuture返回一个布尔值。然后,我让这段代码在数据模板中工作,以设置背景样式

<DataTemplate x:Key="MeetingListItemTemplate">
    <Grid x:Name="grid">
          <!-- Removed lots of stuff here-->
    </Grid>
    <DataTemplate.Triggers>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding Path=IsFuture}" Value="True"/>
            </MultiDataTrigger.Conditions>
            <MultiDataTrigger.Setters>
                <Setter Property="Background" Value="#0094d6" TargetName="grid"/>
            </MultiDataTrigger.Setters>
        </MultiDataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>


我设置了MutlidatTrigger,这样我就可以有交替索引的条件,然后我会为每个交替使用不同的蓝色,但我不确定如何从这里获得交替索引。有什么想法吗?

您应该能够使用
相对资源
绑定来绑定父
项控件
替代索引
属性:

<MultiDataTrigger.Conditions>
    <Condition Binding="{Binding Path=IsFuture}" Value="True" />
    <Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}, Path=AlternationIndex}" Value="1"
</MultiDataTrigger.Conditions>



尝试TemplatedParent RelativeSource模式

您是否尝试过使用RelativeSource绑定来获取父项控件的AlternationIndex属性?这是我收集到的我需要做的,但我对XAML还是比较陌生,不确定如何正确绑定到它。
<MultiDataTrigger.Conditions>
     <Condition Binding="{Binding Path=IsFuture}" Value="true" />
     <Condition Binding="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=(ItemsControl.AlternationIndex)}"  Value="0"/>
</MultiDataTrigger.Conditions>