#198 – Creating and Showing Additional Windows

You can create and show additional Window objects at runtime–in addition to your application’s main window.

Let’s assume that we want a second type of window in our application, beyond the MainWindow class that the WPF Application wizard creates for us.  To start with, right-click in the Solution Explorer and select Add, New Item.

In the dialog, that comes up, select WPF as the template group and then Window (WPF) as the object to add.  Give the new window a name.

It will now show up in the Solution Explorer.

Finally, to create and show the new window, you just create an instance of the new class and call its Show method.  For example, we might add a Button on the main form that creates a new window whenever you click on it.

    private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        AnotherWindow win = new AnotherWindow();
        win.Show();
    }
Advertisements