I am trying to change the background color of the selected items in a list box. I did that before by using
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Yellow" />
which worked. But if I set IsEnabled to false on the ListBox, the whole background of the ListBox turns to the specified color of the ControlBrush. I only want to have the color changed if a ListBoxItem is selected and the ListBox has no focus.
I tried some varieties with triggers, but I can't get it to work. Even a multitrigger containing conditions with IsSelected and IsFocused didn't work for me.
Does anyone have a solution for me?
Edit: Tried the example with the ItemContainerStyle where I am getting a NullReferenceException in my project. In a new solution it works. That's the code where it doesn't work:
<ItemsControl ItemsSource="{Binding Path=Classification.Values}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" IsEnabled="{Binding Path=ClassificationEnabled}"
VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" Grid.Row="0" x:Name="measureClassificationControl">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="2">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Category}"/>
<ListBox ItemsSource="{Binding Values.SortedList}" SelectionMode="Extended" Grid.Row="1" AlternationCount="2"
SelectionChanged="ListBox_SelectionChanged" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Height="120">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock TextWrapping="NoWrap" Text="{Binding Key}">
<TextBlock.ToolTip>
<ToolTip>
<TextBlock TextWrapping="NoWrap" Text="{Binding Value}"/>
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="2"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>