Application.Windows Property

Definition

Gets the instantiated windows in an application.

public System.Windows.WindowCollection Windows { get; }

Property Value

A WindowCollection that contains references to all window objects in the current AppDomain.

Examples

The following example demonstrates how to enumerate the Windows property to build a top-level Windows menu, which is common to multiple-document interface (MDI) applications like Microsoft Excel, or multiple-instance Single Document Interface (SDI) applications like Microsoft Word.

using System;
using System.Windows;
using System.Windows.Controls;

namespace CSharp
{
    // Custom menu item that stores a reference to a window
    public class WindowMenuItem : MenuItem
    {
        public Window Window = null;
    }
}
<Window x:Class="CSharp.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="CSharp" Height="300" Width="300" Activated="MainWindow_Activated"
    >
  <StackPanel>
    <Menu>
      <MenuItem Header="_File">
        <MenuItem Name="newWindowMenuItem" Click="newWindowMenuItem_Click" Header="_New Window"></MenuItem>
        <Separator></Separator>
        <MenuItem Name="exitMenuItem" Click="exitMenuItem_Click" Header="E_xit"></MenuItem>
      </MenuItem>
      <MenuItem Name="windowMenuItem" Header="_Window">
      </MenuItem>
    </Menu>
    <Canvas></Canvas>
  </StackPanel>
</Window>
using System;
using System.Windows;
using System.Windows.Controls;

namespace CSharp
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        void MainWindow_Activated(object sender, EventArgs e)
        {
            this.windowMenuItem.Items.Clear();
            int windowCount = 0;
            foreach (Window window in Application.Current.Windows)
            {
                ++windowCount;
                WindowMenuItem menuItem = new WindowMenuItem();
                menuItem.Window = window;
                menuItem.Header = "_" + windowCount.ToString() + " Window " + windowCount.ToString();
                menuItem.Click += new RoutedEventHandler(menuItem_Click);
                this.windowMenuItem.Items.Add(menuItem);
            }
        }

        void menuItem_Click(object sender, RoutedEventArgs e)
        {
            WindowMenuItem menuItem = (WindowMenuItem)sender;
            menuItem.Window.Activate();
        }
    }
}

Remarks

A Window reference is automatically added to Windows as soon as a window is instantiated on the user interface (UI) thread; windows that are created by worker threads are not added. A Window reference is automatically removed after its Closing event has been handled and before its Closed event is raised.

By default, the first item added to the Windows property becomes the MainWindow.

This property is available only from the thread that created the Application object.

Applies to

Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also