Skip to main content

Window

Window is a top-level ContentControl that represents an operating system (OS) window. It provides the title bar, icon, and system chrome, i.e., close, minimize and maximize buttons.

This page covers technical reference information for the Window control. For practical guidance on using Window, see How to: Work with windows.

tip

Window is only available on desktop platforms, i.e., Windows, macOS and Linux. If you are targeting mobile or browser, use UserControl with a navigation framework instead.

When to use​

The default Avalonia MVVM Application template includes only a main window. If you are making a single-view app, this is usually all you need. You can change what the user sees by swapping content inside the main window, without having to open new windows.

Add new windows when some content needs to exist separately from the main window. For example:

  • Dialogs that ask for input or confirmation before the user can continue, such as a "Save changes?" prompt. Open these with ShowDialog.
  • Settings or preferences screens that the user may open now and then.
  • Tool windows that should stay open beside the main window, such as palettes, inspectors, or property panels.
  • Document windows in apps where users can work on multiple files at once, each in its own window.
  • Views on other monitors, such as a presenter view on one screen and full-screen output on another.

A new window does not inherit the data context of the window that opens it. Each window is the root of its own logical tree, so its DataContext is null until you set it. When creating a new window, always remember to give it a DataContext, usually a view model of its own. See Setting the data context for more information.

Common properties​

PropertyTypeDescription
TitlestringText displayed in the title bar.
IconWindowIcon?Icon displayed in the title bar and taskbar. Ignored on macOS, where the Dock icon comes from the app bundle.
HeightdoubleHeight of the window.
WidthdoubleWidth of the window.
MaxHeightdoubleMaximum allowed height of the window.
MaxWidthdoubleMaximum allowed width of the window.
MinHeightdoubleMinimum allowed height of the window.
MinWidthdoubleMinimum allowed width of the window.
PositionPixelPointPosition of the window in screen coordinates.
CanResizeboolWhether the user can resize the window. Defaults to True.
CanMinimizeboolWhether the user can minimize the window. Defaults to True.
CanMaximizeboolWhether the user can maximize the window. Defaults to True. Always False if CanResize is False.
SizeToContentSizeToContentControls whether the window auto-sizes to fit its content. Height, Width, WidthAndHeight, or Manual.
WindowStartupLocationWindowStartupLocationWhere the window appears on startup. Manual, CenterScreen, or CenterOwner.
WindowStateWindowStateCurrent state of the window. Normal, Minimized, Maximized or FullScreen.
IsDialogboolWhether the window is a dialog. True if opened with ShowDialog, False if opened with Show.
ShowInTaskbarboolWhether the window appears in the OS taskbar.
TopmostboolWhether the window stays above all other windows.
WindowDecorationsWindowDecorationsWhether to show the window chrome and/or borders. Full, BorderOnly, or None.
ExtendClientAreaToDecorationsHintboolWhen True, your content extends into the title bar area so you can create custom chrome with WindowDrawnDecorations.
TransparencyLevelHintIReadOnlyList<WindowTransparencyLevel>Window transparency. None, Transparent, Blur, AcrylicBlur, Mica. Default is an empty array.

See How to work with windows for details.
ClosingBehaviorWindowClosingBehaviorHow child windows behave when the owner window closes.

OwnerAndChildWindows is the default and closes children before the owner. If a child cancels the close, the owner's close is also cancelled.

OwnerWindowOnly closes the owner directly. Children are also closed, but their Closing events are skipped so they cannot cancel.

Platform differences​

FeatureWindowsmacOSLinux
TopmostSupportedSupportedSupported
TransparencyLevelHintAll levelsTransparent and native blur (mapped to AcrylicBlur)Depends on compositor
WindowDecorations.NoneSupportedSupportedSupported
ExtendClientAreaToDecorationsHintSupportedSupportedLimited support

Window events​

EventWhen it fires
OpenedWindow is shown.
ClosingWindow is about to close. Can be cancelled.
ClosedWindow has closed.
ActivatedWindow has received focus.
DeactivatedWindow has lost focus.
PositionChangedWindow was moved.
ResizedWindow was resized.

Methods​

For examples of how to use these methods, see How to: Work with windows.

MethodDescription
Show()Opens the window. Throws InvalidOperationException if the window has already been closed.
Show(Window owner)Opens the window as a child of owner.
ShowDialog(Window owner)Opens the window as a modal dialog that belongs to owner. Returns a Task that completes when the dialog closes.
ShowDialog<TResult>(Window owner)Opens the window as a modal dialog that belongs to owner. Returns a Task<TResult> that produces the value passed to Close(object?).
Close()Closes the window and its child windows. Raises the Closing event. A closed window cannot be shown again.
Close(object? dialogResult)Closes the window and sets the result that ShowDialog<TResult> produces.
Hide()Hides the window and its child windows without closing them. Call Show to display the window again.
Activate()Brings the window to the front and gives it focus.
BeginMoveDrag(PointerPressedEventArgs e)Starts moving the window with the pointer. Call it from a PointerPressed handler for the left mouse button.
BeginResizeDrag(WindowEdge edge, PointerPressedEventArgs e)Starts resizing the window from the given edge. Call it from a PointerPressed handler for the left mouse button.

Notes​

  • Startup window. Your application's main window is set in App.axaml.cs by assigning MainWindow on the IClassicDesktopStyleApplicationLifetime. See Main window for details.
  • Multiple windows. You can open as many windows as you need by creating new instances and calling Show. Each window runs independently within the same application.
  • Closing behavior. When the last window closes, your application exits by default. You can change this by setting ShutdownMode on the application lifetime. Note that hidden windows count as open and may keep the application running invisibly.

See also​