Show multiple Windows at startup in WPF


There are often times when you need to show multiple Windows when a WPF application starts.

When you create a WPF application, your App.xaml looks like this. As you can see from this XAML code, the StartupUri is set to Window1.xaml. That means, when your application runs, Window1.xaml is the first Window that is shown.

<Application x:Class="WPFSample.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>

Now let's say, you have one more window called SecondWindow.xaml. You want both of these Windows to be shown when the application starts.

This can be achieved by calling SecondWindow on the application startup event. To write the startup event handler, I add Startup="App_Startup" to App.xaml.

<Application x:Class="WPFSample.App"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             StartupUri="Window1.xaml"

             Startup="App_Startup">

    <Application.Resources>

        

    </Application.Resources>

</Application>

 

Now I write this event handler in the App class and create an instance of SecondWindow, set its position by using Top and Left properties and call its Show method to make it visible.

  public partial class App : Application

    {

        void App_Startup(object sender, StartupEventArgs e)

        {

            SecondWindow mainWindow = new SecondWindow();

            mainWindow.Top = 100;

            mainWindow.Left = 400;

            mainWindow.Show();

        }

    }

Now if you run the application, you will see two Windows.

TwoWindows.gif

Using the same approach, you can show as many Windows you would like on the application startup.


Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.