设置Silverlight列表框的背景色

时间:2020-03-06 14:56:56  来源:igfitidea点击:

如何设置列表框的背景色?我有一个带有文本块的列表框,但似乎没有任何设置可以实际设置这些控件的背景色,为什么这看起来这么难?

为了全面披露,我早些时候问过类似的问题

解决方案

我们可以使用ListBox.ItemContainerStyle属性来执行此操作。可以在这里找到非常好的解释。基于该示例,我们可以将ItemContainterStyle设置为具有透明的背景色,然后将ListBox包裹在Border中(ListBox不显示其背景色)。

<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>

如果只想设置实际项目,则可以将"背景"设置为实际颜色,然后跳过边框。