Application.Windows Property


Gets the instantiated windows in an application.

Namespace:   System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)

Public ReadOnly Property Windows As WindowCollection

Property Value

Type: System.Windows.WindowCollection

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

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.

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.

Imports Microsoft.VisualBasic
Imports System
Imports System.Windows
Imports System.Windows.Controls

Namespace VisualBasic
    ' Custom menu item that stores a reference to a window
    Public Class WindowMenuItem
        Inherits MenuItem
        Public Window As Window = Nothing
    End Class
End Namespace
<Window x:Class="CSharp.MainWindow"
  Title="CSharp" Height="300" Width="300" Activated="MainWindow_Activated"
      <MenuItem Header="_File">
        <MenuItem Name="newWindowMenuItem" Click="newWindowMenuItem_Click" Header="_New Window"></MenuItem>
        <MenuItem Name="exitMenuItem" Click="exitMenuItem_Click" Header="E_xit"></MenuItem>
      <MenuItem Name="windowMenuItem" Header="_Window">

Imports Microsoft.VisualBasic
Imports System
Imports System.Windows
Imports System.Windows.Controls

Namespace VisualBasic
    Partial Public Class MainWindow
        Inherits Window
        Public Sub New()
        End Sub
        Private Sub MainWindow_Activated(ByVal sender As Object, ByVal e As EventArgs)
            Dim windowCount As Integer = 0
            For Each window As Window In Application.Current.Windows
                windowCount += 1
                Dim menuItem As New WindowMenuItem()
                menuItem.Window = window
                menuItem.Header = "_" & windowCount.ToString() & " Window " & windowCount.ToString()
                AddHandler menuItem.Click, AddressOf menuItem_Click
            Next window
        End Sub

        Private Sub menuItem_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            Dim menuItem As WindowMenuItem = CType(sender, WindowMenuItem)
        End Sub
    End Class
End Namespace

.NET Framework
Available since 3.0
Available since 5.0
Return to top