You can use ContentControl to host your UserControl:
<ItemsControl ItemsSource="{Binding ViewList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentControl Content="{Binding Name,Converter={StaticResource NameToContentConverter}}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Define ObservableCollection:
public ObservableCollection<object> ViewList { get; set; } =
new ObservableCollection<object>();
and to add Content later
ViewList.Add(new View() { Name = "yourUserControlName" });
Your View Class:
public class View
{
public string Name { get; set; } = "";
}
Since ContentControl.Content expect object and you are passing it as a string
you can use Converter.
Converter:
public class NameToContentConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value != null)
{
Type userControl = Type.GetType(System.Reflection.Assembly.GetExecutingAssembly().GetName().Name +"."+ value,null,null);
return Activator.CreateInstance(userControl);
}
return "";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
to know more about Activator see here
