我将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>
基本上,我正在尝试做的是交替每个其他项目的背景颜色,这是有效的。这是一张图片:
然而,当我想要为所选项目的背景着色时会出现问题,在这种情况下,我选择红色用于测试目的,以及灰色边框画笔。结果如下:
如您所见,我选择了“项目009”,但背景未更改为红色。唯一改变的是边框颜色。如果我禁用AlternationIndex触发器,背景将正确着色。这让我相信,由于某种原因,AlternationIndex触发器优先于IsSelected触发器,或者在IsSelected Trigger之后触发,因此我看不到红色背景。
我的问题是:如何修复我的实现以绕过IsSelected触发器的明显覆盖,为我的背景着色着色,同时还保持所需的AlternationIndex着色?
答案 0 :(得分:1)
<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>