I think a better approach might be to define a DataTemplate
for each ViewModel type you want to use. Then, you can just add the ViewModel objects themselves to the WrapPanel's items.
The WPF infrastructure will see there is a DataTemplate for this type of object and will take care of showing the associated DataTemplate.
You need to declare the DataTemplate in some scope that is accessible to your WrapPanel, for example in the Resources of a parent page/window or in App.xaml
's Resources:
<DataTemplate DataType="{x:Type local:YourViewModelClass}">
<ComboBox>
<!-- Define your bindings and stuff here -->
</ComboBox>
</DataTemplate>
Note that WrapPanel
doesn't have an ItemsSource
property, so if you want to bind its items to a list of objects (like your ViewModels), you'll need to use an ItemsControl
:
<ItemsControl ItemsSource="{Binding YourListOfViewModels}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
Assuming YourListOfViewModels
is an ObservableCollection
of your ViewModels, each time you add a ViewModel to that list, it will be displayed with the correct DataTemplate
.