Posted by : Sudhir Chekuri Wednesday, 2 October 2013

Steps to navigate from one WPF window to other windows using C# code on button click

Now you have a window named as MainWindow in your application.
Create a new window to navigate to that window on button click

Steps to create new window

Go to solution explorer
Right click on the root folder(FirstWPF)
Add - window
Leave the default name as window1 and click on ok

Now write the below code in Button1_Click in MainWindows.xaml.cs file to navigate to window1 when the user click on button.

 private void Button1_Click(object sender, RoutedEventArgs e)
        {
            Window1 w = new Window1();
            w.Show();
            this.Hide();
        }

Explanation

Created an object named w for Window1 class.
Show method is used to display Window1 and this.Hide is used to make the present window(MainWindow) invisible.

Now we have two windows and when the application is executed by default MainWindow is displayed first.
But if you want to display Window1 first follow the below steps.

Steps to change the startup window in WPF

Go to solution explorer.
Open app.xaml page
Modify the Startup Uri to Window1.xaml.
Now when the app is executed you can see that Window1 is launched first.

<Application x:Class="FirstWPF.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="Window1.xaml">
    <Application.Resources>
         
    </Application.Resources>
</Application>






Leave a Reply

Subscribe to Posts | Subscribe to Comments

Followers

Total Pageviews

Powered by Blogger.

Blog Archive

- Copyright © 2013 DevStudent - Metrominimalist - Powered by Blogger - Designed by Johanes Djogan -