How can I make listbox transparent, but listbox items opaque in WPF?

Tomas Vinter picture Tomas Vinter · Nov 1, 2009 · Viewed 13.8k times · Source

I'm trying to create a transparent ListBox in a WPF application. I want the ListBox to be completely transparent, thus a background image is visible "behind" the ListBox. However, I want my ListBox items to be totaly opaque, that is to say, they lay on top of the background image.

Do anyone know how I can accomplish this?

Thanx in advance!

Answer

Drew Marsh picture Drew Marsh · Nov 1, 2009

Sure, it's as simple as setting the Background and BorderBrush properties on the ListBox to Transparent and then setting a Background for the ListBoxItems:

<StackPanel Background="Red">
    <ListBox Background="Transparent" BorderBrush="Transparent">
        <ListBox.Resources>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Background" Value="White" />
                <Setter Property="Margin" Value="1" />
            </Style>
        </ListBox.Resources>
        <ListBoxItem Content="First Item"/>
        <ListBoxItem Content="Secton Item"/>
    </ListBox>
</StackPanel>

NOTE: I added a Margin to the ListBoxItems just to demostrate the spacing between the ListBoxItems will show all the way through to the surrounding StackPanel's red background.