本文介绍了将背景图像设置为列表框项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用background属性将图像设置为Listbox.但是,如何设置所需的图像作为列表框项目的背景呢?

I'm able to set an image to Listbox using the background property.But how do I set a desirable image as a background to items of the listbox?

谢谢.

推荐答案

您必须重新定义ListBox的ItemTemplate属性.如果您对XAML不确定,则应尝试使用Expression Blend.

You'll have to redefine the ItemTemplate property of the ListBox. If you're not confident with XAML, you should try using Expression Blend.

以下是您的XAML外观示例.我使用数据透视模板"应用程序创建了一个新应用程序.

Here is an example of how you're XAML could look like. I created a new application using the Pivot Template application.

        <ListBox x:Name="FirstListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                  <StackPanel Margin="0,0,0,17" Width="432" Height="78">
                    <StackPanel.Background>
                        <ImageBrush Stretch="Fill" ImageSource="/Koala.jpg"/>
                    </StackPanel.Background>
                      <TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                      <TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                  </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

因此默认的ItemTemplate使用StackPanel作为主容器.您要在此处执行的操作是将图像设置为该StackPanel的背景.这就是以下几行所代表的意思:

So the default ItemTemplate uses a StackPanel as main container. What you want to do here, is to set the image as the background of that StackPanel. That's what the following lines stands for:

                <StackPanel.Background>
                    <ImageBrush Stretch="Fill" ImageSource="/Koala.jpg"/>
                </StackPanel.Background>

使用上面的代码,将ImageBrush设置为StackPanel的Background属性.

With the above code, you set an ImageBrush as the Background property of the StackPanel.

使用该代码,每个ListBoxItem将显示一个考拉.

With that code, each ListBoxItem will display a koala.

这篇关于将背景图像设置为列表框项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 13:04