Hi Jenny,
yes it is possible. You can load UserControl from file and display it in ContentControl. Try following demo. Window87UC1.xaml and Window87UC2.xaml are xaml files (UserControl without CodeBehind). This xaml files are included in project as Content.
Window87UC1.xaml
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Label Content="I am UserControl 1" Background="LightGreen"/>
</Grid>
</UserControl>
Window87UC2.xaml
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Label Content="I am UserControl 2" Background="LightPink"/>
</Grid>
</UserControl>
XAML of MainWindow
<Window x:Class="WpfApp1.Window86"
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:WpfApp86"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<StackPanel>
<Button Content="Display UC 1" Command="{Binding}" CommandParameter="UC1" Margin="5"/>
<Button Content="Display UC 2" Command="{Binding}" CommandParameter="UC2" Margin="5"/>
<ContentControl Grid.Row="1" Content="{Binding DisplayUC}" Height="80"/>
</StackPanel>
</Window>
ViewModel:
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
namespace WpfApp86
{
public class ViewModel : ICommand, INotifyPropertyChanged
{
public UserControl DisplayUC { get; set; }
public void Execute(object parameter)
{
DisplayUC = (UserControl)XamlReader.Load(new FileStream($"Window87{parameter}.xaml", FileMode.Open));
OnPropertyChanged(nameof(DisplayUC));
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter) => true;
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propName = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
}
Result: