WPF ItemsControl.AlternationIndex不适用于触发器

时间:2016-06-06 08:09:28

标签: c# wpf xaml listviewitem

我读了很多关于此事的内容,但我无法找到解决问题的方法。 我为ListViewItem定义了一个新样式,但ItemsControl.AlternationIndex的属性没有触发。

<Style TargetType="ListViewItem">
    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    <Setter Property="VerticalContentAlignment" Value="Stretch" />
    <Setter Property="Background" Value="{DynamicResource StandardBackgroundColor}" />
    <Setter Property="Foreground" Value="{StaticResource StandardForegroundColor}" />
    <Style.Triggers>
        <Trigger Property="ItemsControl.AlternationIndex" Value="0">
            <Setter Property="Background" Value="#181818" />
        </Trigger>
        <Trigger Property="ItemsControl.AlternationIndex" Value="1">
            <Setter Property="Background" Value="#626262" />
        </Trigger>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="{StaticResource DisabledGray}" />
        </Trigger>
    </Style.Triggers>
</Style>
<Style x:Key="IOListViewItem" TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource {x:Type ListViewItem}}">
    <Setter Property="Height" Value="60" />
    <Setter Property="FontSize" Value="18" />
    <Setter Property="FontWeight" Value="Bold" />
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="OverridesDefaultStyle" Value="True"></Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <Border Name="Border" Padding="2" SnapsToDevicePixels="true">
                    <ContentPresenter 
                              Focusable="True"
                              Visibility="Visible"
                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                              Margin="{TemplateBinding Padding}"
                              RecognizesAccessKey="True"
                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                        <Setter Property="Background" Value="Red" />
                    </Trigger>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                        <Setter Property="Background" Value="#626262" />
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="{StaticResource DisabledGray}" />
                    </Trigger>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter TargetName="Border" Property="Background" Value="{StaticResource Blue}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

非常感谢!

1 个答案:

答案 0 :(得分:0)

AlternationCount="2"设置ListView以查看替代项目的不同颜色

IOListViewItem样式的ControlTemplate也不使用Backround

<Border Name="Border" Padding="2" 
        Background="{TemplateBinding Background}" 
        SnapsToDevicePixels="true">
相关问题