Silverlight:使列表框背景透明吗?

时间:2010-12-10 22:59:37

标签: silverlight xaml

我有一个ListBox。它有白色背景。我怎么能摆脱它?

这是我正在尝试的XAML。无论我做什么,我都无法摆脱那种背景。 (我不确定它是否出现在每个项目上,这恰好占用了ListBox中的所有空格,或者它是否位于ListBox本身的背景上。)

<ListBox x:Name="topThreeHits" ItemsSource="{Binding TopThreeHits}" Margin="0,10,0,0">
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="Background" Value="Transparent" />
                    </Style>
                </ListBox.ItemContainerStyle>
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" Background="Transparent"/>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="10,0" Background="Transparent">
                            <Image Source="{Binding Image, FallbackValue=/PlumPudding;component/Images/file.png}" />
                            <TextBlock>
                            <Run Text="{Binding Name, FallbackValue='File Name'}" FontWeight="Bold" />
                            <Run Text="." Foreground="#787878" FontWeight="Light" />
                            <Run Text="{Binding TypeExtension, FallbackValue='type'}" Foreground="#787878" FontWeight="Light" />
                            </TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

我正在使用Silverlight 4。

2 个答案:

答案 0 :(得分:3)

您的代码运行正常,并正确设置背景样式。我假设您想要做的是彻底清除默认项目容器,因此没有背景,包括翻转等。

最好的方法就是这样:

     <ListBox.ItemContainerStyle>
      <Style TargetType="ListBoxItem">
        <Setter Property="Template" >
          <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
              <ContentPresenter Content="{TemplateBinding Content}" />
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>
    </ListBox.ItemContainerStyle>

答案 1 :(得分:1)

我尝试使用绿色背景在ListBox周围添加一个边框,并为ListBox设置背景为透明,它似乎工作正常。

<Border Background="Green">
    <ListBox x:Name="topThreeHits"
             Background="Transparent"
             ItemsSource="{Binding Customers}" Margin="0,10,0,0">
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="Background" Value="Transparent" />
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" Background="Transparent"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Margin="10,0" Background="Transparent">  
                    <Image Source="{Binding Image, FallbackValue=/PlumPudding;component/Images/file.png}" />  
                    <TextBlock>  
                    <Run Text="{Binding Name, FallbackValue='File Name'}" FontWeight="Bold" />  
                    <Run Text="." Foreground="#787878" FontWeight="Light" />  
                    <Run Text="{Binding TypeExtension, FallbackValue='type'}" Foreground="#787878" FontWeight="Light" />  
                    </TextBlock>  
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Border>