2

I have a Listbox with an Alternation Index of 2 on it. I then have a style set up to provide styling on the different alternation indexes.

<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>

I also want to change the background on the items if they happen in the future. I have a function IsFuture returning a boolean. I then got this code working in the data template to style the background.

<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>

I set up the MutliDataTrigger so I could have the condition for alternation index and then I would use a different blue for each alternation but I'm not sure how I can get alternation index from here. Any ideas?

4

2 回答 2

3
<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 模式。

于 2012-04-18T19:52:51.763 回答
0

您应该能够使用RelativeSource绑定来绑定到AlternationIndexparent 的属性ItemsControl

<MultiDataTrigger.Conditions>
    <Condition Binding="{Binding Path=IsFuture}" Value="True" />
    <Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}, Path=AlternationIndex}" Value="1"
</MultiDataTrigger.Conditions>
于 2012-04-12T21:52:48.543 回答