WPF UserControl或ControlTemplate ...(不确定)

时间:2012-04-04 19:47:31

标签: wpf xaml wpf-controls

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

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

我不想在ListBox.Items中重复20次我希望有一些UserControl我可以做的事情如下所示我可以设置一些自定义属性:

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

由于

2 个答案:

答案 0 :(得分:4)

如果您仅将其用于该SPECIFIC列表框,则只需指定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}

答案 1 :(得分:0)

不是我最喜欢的方法,因为它使用XmlDataProviderXPath语法(我总是会忘记)。但您可以将静态数据作为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

希望这有帮助!