ListViewItem MultiDataTrigger 和 AlternationIndex

ListViewItem MultiDataTrigger and AlternationIndex

所以我尝试为每个应用不同的 Background 颜色 ListViewItem:

<Style.Triggers>
    <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
            <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="False"/>
            <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="False"/>
            <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=AlternationIndex}" Value="0"/>
        </MultiDataTrigger.Conditions>
        <Setter Property="Background" Value="Blue"/>
    </MultiDataTrigger>
    <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
            <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="False"/>
            <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="False"/>
            <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=AlternationIndex}" Value="1"/>
        </MultiDataTrigger.Conditions>
        <Setter Property="Background" Value="Red"/>
    </MultiDataTrigger>
</Style.Triggers>

但这似乎完全不影响我的 ListViewItem,而且看起来都一样。

更新

<Style x:Key="ListViewItemStyle" TargetType="{x:Type ListViewItem}">
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="FontSize" Value="12"/>
    <Setter Property="Height" Value="40"/>
    <Setter Property="Cursor" Value="Hand"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <Border BorderBrush="{TemplateBinding BorderBrush}" 
                        BorderThickness="{TemplateBinding BorderThickness}" 
                        Background="{TemplateBinding Background}" 
                        CornerRadius="2"
                        SnapsToDevicePixels="true">
                    <Border x:Name="InnerBorder"
                            BorderThickness="0"
                            CornerRadius="1">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition MaxHeight="11"/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <GridViewRowPresenter Grid.RowSpan="2"
                                                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Grid>
                    </Border>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>

        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=(ItemsControl.AlternationIndex)}" Value="0"/>
                <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="False"/>
                <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="False"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Background" Value="Blue"/>>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=(ItemsControl.AlternationIndex)}" Value="1"/>
                <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="False"/>
                <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="False"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Background" Value="Red"/>
        </MultiDataTrigger>
    </Style.Triggers>
</Style>

如果您只在您的条件中使用 ListBoxItem 的属性(或附加属性),您可以使用 MultiTrigger instead of a MultDataTrigger.

来简化您的样式
<Style.Triggers>
   <MultiTrigger>
      <MultiTrigger.Conditions>
         <Condition Property="IsMouseOver" Value="False"/>
         <Condition Property="IsSelected" Value="False"/>
         <Condition Property="ItemsControl.AlternationIndex" Value="0"/>
      </MultiTrigger.Conditions>
      <Setter Property="Background" Value="Blue"/>
   </MultiTrigger>
   <MultiTrigger>
      <MultiTrigger.Conditions>
         <Condition Property="IsMouseOver" Value="False"/>
         <Condition Property="IsSelected" Value="False"/>
         <Condition Property="ItemsControl.AlternationIndex" Value="1"/>
      </MultiTrigger.Conditions>
      <Setter Property="Background" Value="Red"/>
   </MultiTrigger>
</Style.Triggers>

否则,使用附带的 属性 绑定语法和括号 AlternationIndex,它是 ItemsControl 的附加 属性,完全限定它:(ItemsControl.AlternationIndex).

<Style.Triggers>
   <MultiDataTrigger>
      <MultiDataTrigger.Conditions>
         <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="False"/>
         <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="False"/>
         <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=(ItemsControl.AlternationIndex)}" Value="0"/>
      </MultiDataTrigger.Conditions>
      <Setter Property="Background" Value="Blue"/>
   </MultiDataTrigger>
   <MultiDataTrigger>
      <MultiDataTrigger.Conditions>
         <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="False"/>
         <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="False"/>
         <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=(ItemsControl.AlternationIndex)}" Value="1"/>
      </MultiDataTrigger.Conditions>
      <Setter Property="Background" Value="Red"/>
   </MultiDataTrigger>
</Style.Triggers>

来自 documentation 关于附加属性的绑定语法:

To bind to an attached property, place parentheses around the attached property. For example, to bind to the attached property DockPanel.Dock, the syntax is Path=(DockPanel.Dock).


更新您的评论和更新:您必须在文档中设置 AlternationCount property on your ListView to the number of alternations explicitly - in you case two - otherwise the default value will be used, which is zero. If the alternation count is zero, the alternation index will also be zero for all items, see Remarks