如何在WPF中使ListBox透明,但ListBox项目不透明?

12

我正在尝试创建一个透明的WPF应用程序中的ListBox。 我希望ListBox完全透明,因此可以看到背景图像“在”ListBox后面。 但是,我希望我的ListBox项目完全不透明,也就是说,它们位于背景图像上方。

有人知道我如何实现这个吗?

提前感谢!


将列表的背景颜色设置为透明,并将样式应用于项目,其中将背景颜色设置为白色。希望这能起作用。 - Ilya Khaprov
1个回答

25

当然,只需要在ListBox上设置Background和BorderBrush属性为透明,并为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>

注意: 我添加了一个Margin到ListBoxItems仅仅是为了演示ListBoxItems之间的间距会一直显示到周围StackPanel的红色背景中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接