Hi Imilio,
you can use ItemsControl to show UserControl like in following demo:
XAML:
<Window x:Class="WpfApp1.Window010"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp010"
mc:Ignorable="d"
Title="Imilio_220324" Height="450" Width="800">
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<Window.Resources>
<Style TargetType="Button">
<Setter Property="Margin" Value="5"/>
<Setter Property="Height" Value="25"/>
<Setter Property="Background" Value="#d0d0ff"/>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions >
<RowDefinition Height="90"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TabControl Grid.Row="0">
<TabItem Header="TabItem1">
<StackPanel Orientation="Horizontal" Background="LightGreen">
<Button Content="Button1" Command="{Binding}" CommandParameter="Button1"/>
<Button Content="Button2" Command="{Binding}" CommandParameter="Button2"/>
<Button Content="Button3" Command="{Binding}" CommandParameter="Button3"/>
</StackPanel>
</TabItem>
</TabControl>
<ItemsControl Grid.Row="1" ItemsSource="{Binding UserControls}"/>
</Grid>
</Window>
ViewModel:
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace WpfApp010
{
public class ViewModel : ICommand
{
public ObservableCollection<FrameworkElement> UserControls { get; set; } = new ObservableCollection<FrameworkElement>();
public event EventHandler? CanExecuteChanged;
public bool CanExecute(object? parameter) => true;
public void Execute(object? parameter)
{
UserControls.Clear();
switch (parameter?.ToString())
{
case "Button1":
UserControls.Add(GetUserControlInstance("Button1"));
break;
case "Button2":
UserControls.Add(GetUserControlInstance("Button2"));
break;
case "Button3":
UserControls.Add(GetUserControlInstance("Button3"));
break;
default:
break;
}
}
private UserControl GetUserControlInstance(string name)
{
Random rnd = new Random();
UserControl ctrl = new UserControl();
Grid grd = new Grid() { Height = 100 };
ctrl.Content = grd;
grd.Background = new SolidColorBrush(Color.FromArgb(255,(byte)rnd.Next(100,255), (byte)rnd.Next(100, 255), (byte)rnd.Next(100, 255)));
grd.Children.Add(new Label() { Content= name });
return ctrl;
}
}
}