A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
You could refer to the following code to switch UserControl.
MainWindow.xaml:
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Grid.Column="0" Command="{Binding ChangeFirstViewCommand}">Change View 1</Button>
<Button Grid.Row="0" Grid.Column="1" Command="{Binding ChangeSecondViewCommand}">Change View 2</Button>
<ContentControl Grid.Row="1" Grid.ColumnSpan="2" Content="{Binding ContentView}"></ContentControl>
</Grid>
MainWindow.xaml.cs:
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
namespace ClickButtonToSwitchUserControl
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
public class ViewModel: INotifyPropertyChanged
{
private FrameworkElement contentView;
public FrameworkElement ContentView
{
get { return contentView;}
set
{
contentView=value;
OnPropertyChanged("ContentView");
}
}
public ViewModel()
{
SwitchViewMessage svm=new SwitchViewMessage();
SwitchView(svm.ViewName);
}
public ICommand ChangeFirstViewCommand
{
get
{
return new RelayCommand(x =>
{
SwitchView("FirstView");
});
}
}
public ICommand ChangeSecondViewCommand
{
get
{
return new RelayCommand(x =>
{
SwitchView("SecondView");
});
}
}
public void SwitchView(string viewName)
{
switch (viewName)
{
case "FirstView" :
ContentView =new FirstView();
ContentView.DataContext=new FirstViewModel() { Text= "This is the first view" };
break;
default:
ContentView = new SecondView();
ContentView.DataContext = new SecondViewModel() { Text = "This is the second View" };
break;
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class SwitchViewMessage
{
public string ViewName { get; set; }
}
public class RelayCommand : ICommand
{
private readonly Predicate<object> _canExecute;
private readonly Action<object> _execute;
public RelayCommand(Action<object> execute)
: this(execute, null)
{
_execute = execute;
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
{
throw new ArgumentNullException("execute");
}
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute == null || _canExecute(parameter);
}
public void Execute(object parameter)
{
_execute(parameter);
}
public event EventHandler CanExecuteChanged
{
add
{
CommandManager.RequerySuggested += value;
CanExecuteChangedInternal += value;
}
remove
{
CommandManager.RequerySuggested -= value;
CanExecuteChangedInternal -= value;
}
}
private event EventHandler CanExecuteChangedInternal;
public void RaiseCanExecuteChanged()
{
CanExecuteChangedInternal.Raise(this);
}
}
public static class EventRaiser
{
public static void Raise(this EventHandler handler, object sender)
{
if (handler != null)
{
handler(sender, EventArgs.Empty);
}
}
}
}
FirstView.xaml:(UserControl1)
<StackPanel>
<Label Content="View 1" FontSize="30"/>
<Button
Content="Goto View 2"
Command="{Binding GotoView2Command}"
HorizontalAlignment="Center"
Margin="10,10,0,0"
VerticalAlignment="Center"
Width="75">
</Button>
</StackPanel>
View1ViewModel:
<StackPanel>
<Label>This is the first view</Label>
<Label Content="{Binding Text}" FontSize="30"/>
</StackPanel>
FirstViewModel:
public class FirstViewModel : INotifyPropertyChanged
{
private string _text;
public string Text
{
get { return _text; }
set
{
_text = value;
OnPropertyChanged("Text");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
SecondView.xaml:(UserControl2)
<StackPanel>
<Label>This is the second view</Label>
<Label Content="{Binding Text}" FontSize="30" />
</StackPanel>
SecondViewModel :
public class SecondViewModel : INotifyPropertyChanged
{
private string _text;
public string Text
{
get { return _text; }
set
{
_text = value;
OnPropertyChanged("Text");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
The result:

If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread.
[5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html