ListBoxItem AlternationIndex触发器覆盖IsSelected触发器

时间:2017-10-30 00:31:39

标签: c# wpf xaml

我将ListboxItem样式定义为:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="VoidwalkerListBoxItem" TargetType="ListBoxItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border
                        Name="_itemContainer"
                        Padding="0"
                        BorderBrush="Transparent"
                        BorderThickness="1"
                        SnapsToDevicePixels="true">
                        <ContentPresenter />
                    </Border>
                    <ControlTemplate.Triggers>
                        <!--
                            Is Selected Triggers
                        -->
                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="_itemContainer" Property="BorderBrush" Value="{DynamicResource VoidwalkerBorderBrush}" />
                            <Setter TargetName="_itemContainer" Property="Background" Value="Red" />
                        </Trigger>

                        <!--
                            Is Mouse Over Triggers
                        -->
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="_itemContainer" Property="BorderBrush" Value="{DynamicResource VoidwalkerBorderBrush}" />
                        </Trigger>
                        <!--
                            Alternation Coloration Triggers
                        -->
                        <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                            <Setter Property="Foreground" Value="{DynamicResource VoidwalkerForegroundBrush}" />
                            <Setter TargetName="_itemContainer" Property="Background" Value="{DynamicResource VoidwalkerContextBrush}" />
                        </Trigger>
                        <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                            <Setter Property="Foreground" Value="{DynamicResource VoidwalkerForegroundBrush}" />
                            <Setter TargetName="_itemContainer" Property="Background" Value="{DynamicResource VoidwalkerControlBrush}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

基本上,我正在尝试做的是交替每个其他项目的背景颜色,这是有效的。这是一张图片:

enter image description here

然而,当我想要为所选项目的背景着色时会出现问题,在这种情况下,我选择红色用于测试目的,以及灰色边框画笔。结果如下:

enter image description here

如您所见,我选择了“项目009”,但背景未更改为红色。唯一改变的是边框颜色。如果我禁用AlternationIndex触发器,背景将正确着色。这让我相信,由于某种原因,AlternationIndex触发器优先于IsSelected触发器,或者在IsSelected Trigger之后触发,因此我看不到红色背景。

我的问题是:如何修复我的实现以绕过IsSelected触发器的明显覆盖,为我的背景着色着色,同时还保持所需的AlternationIndex着色?

1 个答案:

答案 0 :(得分:1)

等等......我觉得自己很蠢。你知道,从来没有一次我认为XAML可能有一个加载/声明顺序。显而易见的问题是触发器是在声明它们的顺序内触发的。这说得通。无论如何,我的问题的一个令人尴尬的简单解决方案如下......首先声明AlternationIndex触发器,然后在XAML中声明IsSelected触发器的最后一个。例如:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="VoidwalkerListBoxItem" TargetType="ListBoxItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border
                        Name="_itemContainer"
                        Padding="0"
                        BorderBrush="Transparent"
                        BorderThickness="1"
                        SnapsToDevicePixels="true">
                        <ContentPresenter />
                    </Border>
                    <ControlTemplate.Triggers>
                        <!--
                            Alternation Coloration Triggers
                        -->
                        <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                            <Setter Property="Foreground" Value="{DynamicResource VoidwalkerForegroundBrush}" />
                            <Setter TargetName="_itemContainer" Property="Background" Value="{DynamicResource VoidwalkerContextBrush}" />
                        </Trigger>
                        <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                            <Setter Property="Foreground" Value="{DynamicResource VoidwalkerForegroundBrush}" />
                            <Setter TargetName="_itemContainer" Property="Background" Value="{DynamicResource VoidwalkerControlBrush}" />
                        </Trigger>
                        <!--
                            Is Selected Triggers
                        -->
                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="_itemContainer" Property="BorderBrush" Value="{DynamicResource VoidwalkerBorderBrush}" />
                            <Setter TargetName="_itemContainer" Property="Background" Value="Red" />
                        </Trigger>

                        <!--
                            Is Mouse Over Triggers
                        -->
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="_itemContainer" Property="BorderBrush" Value="{DynamicResource VoidwalkerBorderBrush}" />
                        </Trigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

Server