使用 MahApps.Metro 的列表框交替行颜色

Alternating row color for Listbox using MahApps.Metro

我在使用 MahApps.Metro 项目时尝试在 WPF 中为我的列表框创建交替行颜色。当我添加样式和触发器时,所有项目的背景仍然是白色:

        <Style TargetType="ListBox">
            <Setter Property="AlternationCount" Value="2" />
            <Style.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="Background" Value="Blue" />
                </Trigger>
            </Style.Triggers>
        </Style>

ListBoxItem 上应用 Style 而不是 ListBox,像这样:

<ListBox AlternationCount="2">
    <ListBox.Resources>
        <Style TargetType="ListBoxItem">
            <Style.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="Background" Value="Blue" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListBox.Resources>
</ListBox>

不要忘记将 AlternationCount 添加到 ListBox,因为它不再在 Style

中设置