Question

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?

Was it helpful?

Solution

Use an ItemsControl and a DataTemplate

<ItemsControl ItemsSource="{Binding YourCollection}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <uc:YourUserControl />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top