【问题标题】:Silverlight: Make the listbox background transparent?Silverlight:使列表框背景透明?
【发布时间】:2011-01-08 23:55:37
【问题描述】:

我有一个列表框。它有一个白色的背景。我怎样才能摆脱它?

这是我正在尝试的 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。

【问题讨论】:

    标签: silverlight xaml


    【解决方案1】:

    您的代码运行良好,并且正确设置了背景样式。我假设你想要做的是完全吹走默认项目容器,所以没有背景,包括翻转等。

    最好的方法是这样的:

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

    【讨论】:

    • 其实我想做的就是去掉背景。我想保持翻转。我将您发布的代码添加到我上面的代码中,现在数据绑定不起作用 - 它填充了默认的后备值。我不知道为什么会这样。
    • 太好了,这正是我想要做的,而不必重新模板列表框的每个属性。简单多了,谢谢。
    【解决方案2】:

    我尝试在列表框周围添加一个带有绿色背景的边框,并将列表框的背景设置为透明,它似乎工作正常。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      相关资源
      最近更新 更多