How to associate an observable collection with an array of user controls?

Data binding in WPF is great, but the moment you try to make things more complex, it becomes extremely difficult to implement things.

I have a collection of objects where each object has observable properties associated with a user control.

I would like (ideally) to simply add a new object to my collection and add a new user control to my form. The fact is that user controls must be dynamically created, so every time I add to the collection, I may need to manually create a new control, set the binding and add it to my window. Is there an easier MVVM way to bind to such a structure?

+3
source share
1 answer

Use ItemsControlandDataTemplate

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

Source: https://habr.com/ru/post/1714850/


All Articles