views:

402

answers:

2

Hey

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?

+1  A: 

Use an ItemsControl and a DataTemplate

<ItemsControl ItemsSource="{Binding YourCollection}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <uc:YourUserControl />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
Thomas Levesque
I had just started looking at this when I read your post.Thanks!
bluebit
A: 

Hi Thomas can you elaborate your statement a bit more with some xaml/vb/cs example, as i am facing the same situation. Scenario is On every button click event, a user control needs to be added with observable collection binded.

See my posting http://stackoverflow.com/questions/2021017/how-to-bind-an-observable-collection-to-multiple-user-controls-at-runtime

Amit Ranjan