Chris
Chris

Reputation: 1146

How can I bind a datatemplate to a observablecollection so that they all show up?

How can I bind a datatemplate to reproduce itself for each item within an observablecollection so that they all show up? Or must I add these as individual controls, at runtime?

So I have a datatemplate:

<UserControl.Resources>
    <DataTemplate x:Key="QueueItem">
        <StackPanel Orientation="Vertical">
            <Label Content="{Binding caseNumber}"></Label>
            <!--TODO: put my other controls for this template here...-->
        </StackPanel>
    </DataTemplate>
</UserControl.Resources>

And I implement it in xaml here:

<ContentControl
               Content="{Binding TestItems}" 
               ContentTemplate="{StaticResource QueueItem}">
</ContentControl>

And in my viewmodel there is a instance TestItems of some class I made, and one member of that is caseNumber, so the code above works just fine for me... But now I would like to have an observable collection of that same class, so is it possible/ is there xaml syntax to bind to the collection instance to reproduce the above for each index?

Thanks!

Upvotes: 0

Views: 1034

Answers (2)

pgenfer
pgenfer

Reputation: 622

You could use an ItemsControl and use the ItemsTemplate property for your template:

<ContentControl>
     <ItemsControl
           ItemsSource="{Binding YourTestItemList}" 
           ItemTemplate="{StaticResource QueueItem}"/>
</ContentControl>

Upvotes: 1

Saurabh Savaliya
Saurabh Savaliya

Reputation: 46

Use ItemsControl instead of ContentControl

Click here for sample code

Upvotes: 1

Related Questions