带数据触发器的交替索引

时间:2012-04-12 18:37:45

标签: xaml binding triggers listbox multidatatrigger

我有一个列表框,其上的转换索引为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>

我设置了MutliDataTrigger,所以我可以有交替索引的条件,然后我会为每次交替使用不同的蓝色,但我不知道如何从这里获得交替索引。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

<MultiDataTrigger.Conditions>
     <Condition Binding="{Binding Path=IsFuture}" Value="true" />
     <Condition Binding="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=(ItemsControl.AlternationIndex)}"  Value="0"/>
</MultiDataTrigger.Conditions>

尝试TemplatedParent RelativeSource模式。

答案 1 :(得分:0)

您应该能够使用RelativeSource绑定绑定到父AlternationIndex的{​​{1}}属性:

ItemsControl