WPF ItemsControl.AlternationIndex在触发器中不起作用

时间:2022-09-09 14:12:26

I read many things about this, but I can't find the solution to my problem. I defined a new style for ListViewItem but the property of ItemsControl.AlternationIndex doesn't trigger.

我读了很多关于这方面的事情,但我无法找到解决问题的方法。我为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>

Thank you very much!

非常感谢你!

1 个解决方案

#1


0  

set AlternationCount="2" for ListView to see different colors for alternate items

为ListView设置AlternationCount =“2”以查看备用项目的不同颜色

also ControlTemplate in IOListViewItem style doesn't use Backround

IOListViewItem样式中的ControlTemplate也不使用Backround

<Border Name="Border" Padding="2" 
        Background="{TemplateBinding Background}" 
        SnapsToDevicePixels="true">

#1


0  

set AlternationCount="2" for ListView to see different colors for alternate items

为ListView设置AlternationCount =“2”以查看备用项目的不同颜色

also ControlTemplate in IOListViewItem style doesn't use Backround

IOListViewItem样式中的ControlTemplate也不使用Backround

<Border Name="Border" Padding="2" 
        Background="{TemplateBinding Background}" 
        SnapsToDevicePixels="true">