Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Changing From MainWindow to Another Page in MVVM Format WPF Application

Tags:

c#

mvvm

wpf

xaml

I have been trying to figure out how to switch from my MainWindow.xaml to a WPF Page in my View folder called StudentView.xaml when a button on the MainWindow is clicked.

This is what I have so far...

private void ButtonStartQuiz_Click(object sender, RoutedEventArgs e) {
    var window = new View.StudentView();
}

All I want is for the MainWindow to switch to the StudentView page when the user clicks the button. I've tried opening the StudentView as a new Window, but I don't want a new window every time the user clicks a button. I've tried googling and looking at other posts, but I don't understand how I'm supposed to implement them. Please help!

like image 451
Saad Avatar asked Jan 31 '26 11:01

Saad


1 Answers

After the button click then, you can create an instance of that page and set the instance as content of your mainwindow.

     private NavigatingPageName Instance;
     private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (Instance == null) 
            {
                Instance = new  NavigatingPageName();
            }
            this.Content = Instance;
        }
like image 81
J. Hasan Avatar answered Feb 03 '26 00:02

J. Hasan