tags:

views:

1695

answers:

2

How do I set the background colour of a listbox? I have a listbox with textblocks in it and there does not appear to be anyway that actually works to set the background colour of these controls, why is this seemingly so hard?

In the interests of full disclosure I asked a similar question earlier

+2  A: 

You can do this using the ListBox.ItemContainerStyle property. Very nice explanation of this can be found here. Based on that example, we can set the ItemContainterStyle to have a transparent background color and then wrap the ListBox in a Border (the ListBox doesn't display its background color).

<Border Background="Green">
<ListBox Background="Red">
  <ListBox.ItemContainerStyle>
      <Style TargetType="ListBoxItem">
          <Setter Property="Background" Value="Transparent"/>
      </Style>
  </ListBox.ItemContainerStyle>
    <TextBlock Text="Hello" />
    <TextBlock Text="Goodbye" />
  </ListBox>
</Border>

If you just want to set the actual items you can set the Background to an actual color and then skip the border.

Bryant
A: 

Thanks Bryant :) Much appreciated!! I spent a stupid amount of time trying to work that out.

Can I bind to the setter value to make the colour choice dynamic?

Dan