WPF, set the background of the Selected ListBox item

  • Thread starter Thread starter AgostinoX
  • Start date Start date
A

AgostinoX

Guest
Hi, I'm trying to set the background of the selected item if a Listbox.

This is my first attempt:


<ListBox>
<ListBoxItem>first</ListBoxItem>
<ListBoxItem>second</ListBoxItem>
<ListBoxItem>third</ListBoxItem>

<ListBox.Resources>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Height" Value="60" />


<!--doesn't work-->
<Setter Property="Background" Value="violet"/>

<Setter Property="Foreground" Value="red"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Resources>

</ListBox>





It sets three properties: Height, Background, Foreground. It works for Height and Foreground, not for Background.

I've looked up the web and the solutions are essentially two:

1) change the system colors at the component level. It seems to be a trick more than a solution, and has its issues too.

2) retemplate the ListBoxItem, that's quite complex.

Is there an 'official' solution, that is also reasonably simple?

Are there plans to simplify this? What are other cases of proprerties that don't work the simple way? I'd better know them in advance.

Why Background behaves differently from the other properties?

Where is this 'exception' mentioned in the documentation?

Thanks in advance

Continue reading...
 
Back
Top