ListBoxItem AlternationIndex Trigger overrides IsSelected Trigger

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 element that works. Here is the image:

enter image description here

However, the problem arises when I want to color the selected background of the element, in this case I chose Red for testing purposes, as well as a gray brush. Here is the result:

enter image description here

As you can see, I selected "Item 009", but the background has not been changed to red. The only thing that has changed is the color of the border. If I disable the AlternationIndex trigger, the background will be colored correctly. This leads me to believe that for some reason, the AlternationIndex trigger has priority over IsSelected Trigger or fired after IsSelected Trigger, so I don't see a red background.

My question is: how can I fix my implementation to get around this apparent redefinition of the IsSelected trigger, by coloring my background in red, while preserving the desired AlternationIndex color?

0
c # wpf xaml
Oct 30 '17 at 0:31
source share
1 answer

Wait ... I feel stupid. You know, I never thought that XAML could have a download / announcement order. The obvious problem is that triggers fire in the order in which they are declared. It makes sense. In any case, the embarrassing simple solution to my problem is this: just declare AlternationIndex triggers first, and then declare IsSelected trigger last in 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> 

enter image description here

0
Oct 30 '17 at 0:38
source share



All Articles