bluebit
bluebit

Reputation: 3017

How to bind an observable collection to an array of user controls?

Databinding in WPF is great, but the moment you try make things more complex it gets exceedingly difficult to implement things.

I have a collection of objects, where each object has observable properties that are bound to a user control.

I would like to (ideally) simply add a new object to my collection, and have a new user control appear on my form. The thing is user controls need to be dynamically created, so each time I add to the collection I may have to manually create a new control, set the binding and add it to my Window. Is there a simpler MVVM style way of binding to such a structure?

Upvotes: 3

Views: 2733

Answers (1)

Thomas Levesque
Thomas Levesque

Reputation: 292425

Use an ItemsControl and a DataTemplate

<ItemsControl ItemsSource="{Binding YourCollection}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <uc:YourUserControl />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Upvotes: 5

Related Questions