WPF UserControl or ControlTemplate... (not sure)

Go To StackoverFlow.com

2

I have a listbox where I have to add about 20 static custom items. All the items are based on the same template (something like that) :

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

I don't want to repeat that 20 times in the ListBox.Items I would like to have some kind of UserControl where I could do something Like the following where I could set some custom properties :

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

But I don't wan't to create a userControl just for that!!! Is there an easy way to put that template in the Window.Resources?

Thanks

2012-04-04 19:47
by danbord


4

If you are ONLY using it for that SPECIFIC listbox, you can just assign the ItemTemplate property. This will need to work in conjunction with a collection of custom objects defined in your resources somewhere else. This will save you from creating a custom UserControl, but you will need an object that can be defined in XAML and a list of them in XAML anyway. To be honest, creating a UserControl is relatively painless and may be easier, but it is possible without doing so.

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

EDIT: If you are going to use it in more than one place, put the DataTemplate in your Application resources and ive it a key, then assign the ItemTemplate property to {StaticResource MyListBoxItemsTemplateKey}

2012-04-04 19:51
by Scott M.
And where did you define the data item object, which provides the ImageSource and TextContent properties - Clemens 2012-04-04 19:54
I am assuming his "20 static custom items" all have the ImageSource and TextContent defined as dependency properties - Scott M. 2012-04-04 19:55
He's adding items in XAML. And just a note: data object properties must not necessarily be dependency properties - Clemens 2012-04-04 19:56
Exactly Clemens, I want to define the items in XAML not using ItemSource nor DataContext - danbord 2012-04-04 20:00
ah, you're right. In the binding they don't need to be dependency properties - Scott M. 2012-04-04 20:01
I can't believe I must create a UserControl for that or have to bind to a collection : - danbord 2012-04-04 20:16
you could also copy and paste the <ListBoxItem><Border>...</Border></ListBoxItem> 20 times, but each time you change something it has to be changed 20 times in the XAML - Scott M. 2012-04-04 20:47


0

Not my favorite approach since it uses the XmlDataProvider and XPath syntax (which I tend to always forget). But you can embed your static data as xml within your Window.Resources like so:

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

Then within your ListBox bind to the XmlDataProvider you specified and use the XPath notation within the bindings to drill down to the data you want the controls to bind to.

This site has a couple good examples too: http://vbcity.com/blogs/xtab/archive/2010/12/24/more-xpath-examples-in-a-wpf-application.aspx

Hope this helps!

2012-04-04 21:13
by shanewwarren
Ads