If you look at the default template of ListBoxItem
, you will see the IsMouseOver
trigger is applied before the IsSelected
trigger. Since DataTrigger
s are evaluated from top to bottom, the last trigger will always win and set the Background
of ListBoxItem
as Transparent
(in your case).
If you want to override that behavior, you'll have to override default template and change ordering of triggers. Below is a sample showing how to do it, you can change background colors and border brushes as per your needs:
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border BorderThickness="{TemplateBinding Border.BorderThickness}"
Padding="{TemplateBinding Control.Padding}"
BorderBrush="{TemplateBinding Border.BorderBrush}"
Background="{TemplateBinding Panel.Background}"
Name="Bd"
SnapsToDevicePixels="True">
<ContentPresenter Content="{TemplateBinding ContentControl.Content}"
ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"
HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
</Border>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive" Value="False"/>
<Condition Property="Selector.IsSelected" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Panel.Background" TargetName="Bd"
Value="Transparent"/>
<Setter Property="Border.BorderBrush" TargetName="Bd">
<Setter.Value>
<SolidColorBrush>#FFDADADA</SolidColorBrush>
</Setter.Value>
</Setter>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive" Value="True"/>
<Condition Property="Selector.IsSelected" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Panel.Background" TargetName="Bd" Value="Transparent"/>
<Setter Property="Border.BorderBrush" TargetName="Bd">
<Setter.Value>
<SolidColorBrush>#FF26A0DA</SolidColorBrush>
</Setter.Value>
</Setter>
</MultiTrigger>
<Trigger Property="UIElement.IsMouseOver" Value="True">
<Setter Property="Panel.Background" TargetName="Bd"
Value="LightSteelBlue"/>
<Setter Property="Border.BorderBrush" TargetName="Bd">
<Setter.Value>
<SolidColorBrush>#A826A0DA</SolidColorBrush>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="UIElement.IsEnabled" Value="False">
<Setter Property="TextElement.Foreground" TargetName="Bd">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" />
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
UPDATE
Maybe I should explain a bit more about my assertions above.
First of all, I had Windows 8 at my disposal and the default templates are quite different between Windows 7 and Windows 8. For Windows 8, theme styles are picked from PresentationFramework.Aero2.dll
, hence the default templates might differ from theme style found under PresentationFramework.Aero.dll
(used for Windows 7 and earlier versions).
I posted above the default template I got from Aero2.dll
. As you can see, it doesn't use HighlightBrushKey
so the suggestion of overriding HighlightBrushKey
doesn't work in this case. (That's why I don't like overriding system brushes, since it might not work for templates using another theme style).
As per Dev's answer
Style triggers take precedence over template triggers anyways so it
wouldn't matter if the template trigger is at first or last place in
list.
I agree that Style
triggers take precedence over template triggers. But, what I mentioned in my answer was the case when you haven't used any Style
triggers and simply provide values in the template. In the default template the MouseOver
trigger is at top and the SelectedItem
trigger below that, so in that case order does matter. You can verify that yourself by moving up the trigger and removing all Style
triggers.
And regarding
I am not sure if the style Rohit posted to you is a complete copy of
the ListBoxItem style. There seem to be style setters missing. He just
posted you the template I guess.
I have posted the complete template of ListBoxItem
found under Aero2.dll
which obviously might differ based on theme style.
I got the style using XamlWriter.Save(listBoxItem.Template, xmlwrite);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…