is it possible to bind an itemscontrol with canvas as template to multiple DataTemplates?
I have 2 collections and depending on the type I would like to display a different control on my canvas.
I am not sure but I could think about a Viewmodel which has 2 ObservableCollections. For example if I would have “Shapes” and “connections” and I would like to display them both on the canvas? In case of a diagraming scenario…
I would like to do this in the mvvm manner and I am not sure if the multiple DataTemplate approach is correct but this came to my mind.
But I am still having problems to get the binding straight in my head. If I set the DataContext to the ViewModel for me it seems not possible to bind 2 collections to the items control… =(
I am also open for other ideas, too….
Is this possible? And if so, how would the binding look like an
You can create multiple
ObservableCollectionsand then bind yourItemsSourceto aCompositeCollectionwhich joins those collections.Then in your XAML you can create different
DataTemplatesfor the respective types using theDataTypeproperty which like styles gets automatically applied if it is placed in the resources. (You can also create the composite in XAML which is shown on MSDN, if theCollectionContainersshould be bound that is a bit more difficult though)Example code:
Here i use a different panel but it should be the same for a canvas.