Why does this not work?
<ListBox>
<ListBox.Items>
<ListBoxItem>Foo</ListBoxItem>
<ListBoxItem>Bar</ListBoxItem>
<ListBoxItem>Text</ListBoxItem>
</ListBox.Items>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True" >
<Setter Property="Background" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
I.e., Background
of ListBoxItem
does not change when it is selected.
I was hoping to achieve this simple behavior without having to override the default item template. Surely there is a simple solution here that I am just not seeing.
Update
The only way I have found which works in both Windows 7 AND Windows 8 is by overriding the default ItemContainerStyle
. This is made slightly less painful in that I can now right-click on the control in Visual Studio and Edit Style - Edit a copy... but this feels like overkill in that I am only ultimately changing the one line of XAML I commented out below. I am only showing a small relevant portion of the complete template I am forced to introduce/override.
Is there not a simpler way?
...
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive" Value="True"/>
<Condition Property="IsSelected" Value="True"/>
</MultiTrigger.Conditions>
<!--<Setter Property="Background" TargetName="Bd" Value="#3D26A0DA"/>-->
<!-- Changed above line to the following line... -->
<Setter Property="Background" TargetName="Bd" Value="Red"/>
<Setter Property="BorderBrush" TargetName="Bd" Value="#FF26A0DA"/>
</MultiTrigger>
...