Dot-Net

如何在 WPF 中使列錶框透明,但列錶框項目不透明?

  • November 1, 2009

我正在嘗試在 WPF 應用程序中創建一個透明的 ListBox。我希望 ListBox 完全透明,因此背景圖像在 ListBox 的“後面”可見。但是,我希望我的 ListBox 項目完全不透明,也就是說,它們位於背景圖像的頂部。

有誰知道我怎麼能做到這一點?

提前謝謝!

當然,這很簡單,只需將 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>

**注意:**我在 ListBoxItems 中添加了一個邊距,只是為了說明 ListBoxItems 之間的間距將一直顯示到周圍的 StackPanel 的紅色背景。

引用自:https://stackoverflow.com/questions/1657621