#200 – Parent/Child Relationships Between Windows

Creating a new Window object and displaying it using the Show method results in a new window in your application that is independent from any existing windows.  The new window can be minimized and maximized independently and gets its own icon on the taskbar.

Creating a new (independent) window:

    Window w = new Window();
    w.Title = DateTime.Now.ToLongTimeString();

WPF supports the notion of parent/child relationships between windows.  You set up the relationship by setting the Owner property of the child window to point to the parent.

    Window w = new Window();
    w.Title = string.Format("Child #{0}", this.OwnedWindows.Count + 1);
    w.Owner = this;

When you make one window the child of another:

  • When a parent is minimized, all the child windows are minimized
  • When child is minimized, parent is not minimized
  • You can interact with either window
  • The parent can’t cover a child window
  • Closing a parent closes all the child windows

#199 – An Application’s Windows Property Lists All of Its Windows

The Application class has a Windows property which is a collection of all of the windows that have been created by the current application.

Assume that we have an application with a main window that includes the following two buttons:

Whenever the user clicks on the Create New Window button, we create and show a new window.

    private void btnCreate_Click(object sender, System.Windows.RoutedEventArgs e)
        AnotherWindow win = new AnotherWindow();
        win.Title = DateTime.Now.ToLongTimeString();

In the Click event handler for the Where Are the Windows? button, we can iterate through all of the windows that the application created and display some information about each one.

        private void btnWhere_Click(object sender, RoutedEventArgs e)
            StringBuilder sb = new StringBuilder();
            foreach (Window w in App.Current.Windows)
                sb.AppendFormat("Window [{0}] is at ({1},{2}).\n", w.Title, w.Top, w.Left);

            MessageBox.Show(sb.ToString(), "My Windows");

The final result:

#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();

#39 – The Window Class

The Window class represents a window in WPF.  It derives from ContentControl, which means that it can contain a single nested element that represents its content.  It inherits directly from Control and indirectly from FrameworkElement, UIElement, Visual, DependencyObject, and DispatcherObject.

A window consists visually of a client area (the inside of the window) and non-client area (title bar, frame and minimize/maximize/close buttons).  It represents the main (outer) visual element that a user interacts with for a WPF standalone application.

You can manage a window’s lifetime through methods like Activate, Close, Hide and Show and events like Activated, Closed, Closing, and Deactivated.

The following XAML:

<Window x:Class="WpfApplication9.MainWindow"
 Title="MainWindow" Height="200" Width="250">
        <Label Content="I'm a WPF Window!" Height="28" HorizontalAlignment="Center" VerticalAlignment="Center" />

Results in this window at runtime: