Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 223
Tomas Vinter Avatar asked Nov 01 '09 16:11

Tomas Vinter


1 Answers

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.

like image 155
Drew Marsh Avatar answered Oct 02 '22 21:10

Drew Marsh