2

我有一个列表框,我必须在其中添加大约 20 个静态自定义项。所有项目都基于相同的模板(类似的东西):

<Border>
  <StackPanel Orientation="Horizontal">
    <Image Source="" Height="30" />
    <TextBlock Text="" VerticalAlignment="Center" />
  </StackPanel>
</Border>

我不想在 ListBox.Items 中重复 20 次我想要某种用户控件,我可以在其中执行某些操作,如下所示,我可以设置一些自定义属性:

<ListBox>
  <ListBox.Items>
    <MyListBoxTemplate x:Name="Item1" ItemText="Item #1" ItemImageSource="/Image1.jpg" />
    <MyListBoxTemplate x:Name="Item2" ItemText="Item #2" ItemImageSource="/Image2.jpg" />
    ...
  </ListBox.Items>
</ListBox>

但我不想为此创建一个 userControl !!!有没有一种简单的方法可以将该模板放入 Window.Resources 中?

谢谢

4

2 回答 2

4

如果您仅将它用于该特定列表框,则只需分配ItemTemplate属性。这将需要与在其他地方的资源中定义的自定义对象集合一起工作。这将使您免于创建自定义 UserControl,但您将需要一个可以在 XAML 中定义的对象以及一个在 XAML 中的列表。老实说,创建​​一个 UserControl 相对来说比较轻松,而且可能更容易,但不这样做也是可能的。

<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate TargetType="CustomObjectType">
            <Border>
              <StackPanel Orientation="Horizontal">
                <Image Source="{Binding ImageSource}" Height="30" />
                <TextBlock Text="{Binding TextContent}" VerticalAlignment="Center" />
              </StackPanel>
            </Border>
        <DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

编辑:如果您要在多个地方使用它,请将其DataTemplate放入您的应用程序资源并为其提供一个键,然后将该ItemTemplate属性分配给{StaticResource MyListBoxItemsTemplateKey}

于 2012-04-04T19:51:32.237 回答
0

不是我最喜欢的方法,因为它使用XmlDataProviderandXPath语法(我往往会忘记)。但是您可以将静态数据作为 xml 嵌入到 Window.Resources 中,如下所示:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Window.Resources>
        <XmlDataProvider x:Key="MyStaticData" XPath="StaticItems" >
            <x:XData>
                <StaticItems xmlns="">
                    <StaticItem>
                        <ItemText>Item #1</ItemText>
                        <ItemImageSource>/Image1.jpg</ItemImageSource>
                    </StaticItem>
                    <StaticItem>
                        <ItemText>Item #2</ItemText>
                        <ItemImageSource>/Image2.jpg</ItemImageSource>
                    </StaticItem>
                </StaticItems>
            </x:XData>
        </XmlDataProvider>
    </Window.Resources>
    <Grid>
        <ListBox>
            <ListBox.ItemsSource>
                <Binding Source="{StaticResource MyStaticData}"  XPath="StaticItem" />
            </ListBox.ItemsSource>
            <ListBox.ItemTemplate>
                <DataTemplate>
                <Border>
                    <StackPanel Orientation="Horizontal">
                            <Image Source="{Binding XPath=ItemImageSource}"  Height="30" />
                            <TextBlock Text="{Binding XPath=ItemText}" VerticalAlignment="Center" />
                        </StackPanel>
                </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

然后在您的 ListBox 中绑定到您指定的 XmlDataProvider,并使用绑定中的 XPath 表示法深入了解您希望控件绑定到的数据。

这个网站也有几个很好的例子:http: //vbcity.com/blogs/xtab/archive/2010/12/24/more-xpath-examples-in-a-wpf-application.aspx

希望这可以帮助!

于 2012-04-04T21:13:47.737 回答