ListBoxItem AlternationIndex 触发器覆盖 IsSelected 触发器 [英] ListBoxItem AlternationIndex Trigger overrides IsSelected Trigger

查看:37
本文介绍了ListBoxItem AlternationIndex 触发器覆盖 IsSelected 触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListboxItem 样式定义为:

I have a ListboxItem Style defined as:

<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>

本质上,我想要做的是交替每个其他项目的背景颜色,这很有效.这是一张图片:

Essentially, what I'm trying to do is alternate the background colors of every other item, which works. Here's a picture:

但是,当我想为所选项目的背景着色时,问题就出现了,在这种情况下,我选择了红色作为测试目的,并使用了灰色边框画笔.结果如下:

However, the problem then arises when I want to color the selected item's background, in this case I chose Red for testing purposes, along with a gray border brush. Here is the result:

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

As you can see, I have selected "Item 009", but the background has not been changed to red. The only thing that has changed is the border color. If I disable the AlternationIndex trigger, the background is colored correctly. This leaves me to believe that for some reason, the AlternationIndex trigger is taking precedence over the IsSelected Trigger, or being fired after the IsSelected Trigger, thus I see no red background.

我的问题是:如何修复我的实现以规避这种明显的 IsSelected 触发器覆盖,将我的背景着色为红色,同时保持所需的 AlternationIndex 着色?

推荐答案

等等...我觉得自己很愚蠢.您知道,我从未考虑过 XAML 可能具有加载/声明顺序.明显的问题是触发器在它们声明的顺序内被触发.这说得通.无论如何,解决我的问题的非常简单如下...只需首先声明 AlternationIndex 触发器,然后在 XAML 中最后声明 IsSelected 触发器.示例:

Wait...I feel stupid. You know, never once did I consider that XAML may have a load/declaration order. The apparent problem is that the triggers are fired within the order in which they're declared. It makes sense. Anyway, an embarrassingly simple solution to my problem is as follows...just declare the AlternationIndex triggers first, then declare the IsSelected trigger last in the XAML. Example:

<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>

这篇关于ListBoxItem AlternationIndex 触发器覆盖 IsSelected 触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆