Table of Contents

Class Window

Namespace
Avalonia.Controls
Assembly
Avalonia.Controls.dll

A top-level window.

public class Window : WindowBase, INotifyPropertyChanged, IDataContextProvider, IThemeVariantHost, IResourceHost, IResourceNode, ISetLogicalParent, ISetInheritanceParent, ISupportInitialize, IStyleable, INamed, IDataTemplateHost, ISetterValue, IRenderRoot, ICloseable, IStyleHost, ILogicalRoot, ILogical, ITextInputMethodRoot, IInputRoot, IInputElement, IFocusScope, ILayoutRoot
Inheritance
Window
Implements
Derived
Inherited Members
Extension Methods

Constructors

Window()

Initializes a new instance of the Window class.

public Window()

Window(IWindowImpl)

Initializes a new instance of the Window class.

public Window(IWindowImpl impl)

Parameters

impl IWindowImpl

The window implementation.

Fields

CanResizeProperty

public static readonly StyledProperty<bool> CanResizeProperty

Field Value

StyledProperty<bool>

ClosingBehaviorProperty

Defines the ClosingBehavior property.

public static readonly StyledProperty<WindowClosingBehavior> ClosingBehaviorProperty

Field Value

StyledProperty<WindowClosingBehavior>

ExtendClientAreaChromeHintsProperty

public static readonly StyledProperty<ExtendClientAreaChromeHints> ExtendClientAreaChromeHintsProperty

Field Value

StyledProperty<ExtendClientAreaChromeHints>

ExtendClientAreaTitleBarHeightHintProperty

public static readonly StyledProperty<double> ExtendClientAreaTitleBarHeightHintProperty

Field Value

StyledProperty<double>

ExtendClientAreaToDecorationsHintProperty

Defines the ExtendClientAreaToDecorationsHint property.

public static readonly StyledProperty<bool> ExtendClientAreaToDecorationsHintProperty

Field Value

StyledProperty<bool>

IconProperty

Defines the Icon property.

public static readonly StyledProperty<WindowIcon?> IconProperty

Field Value

StyledProperty<WindowIcon>

IsExtendedIntoWindowDecorationsProperty

Defines the IsExtendedIntoWindowDecorations property.

public static readonly DirectProperty<Window, bool> IsExtendedIntoWindowDecorationsProperty

Field Value

DirectProperty<Window, bool>

OffScreenMarginProperty

public static readonly DirectProperty<Window, Thickness> OffScreenMarginProperty

Field Value

DirectProperty<Window, Thickness>

ShowActivatedProperty

Defines the ShowActivated property.

public static readonly StyledProperty<bool> ShowActivatedProperty

Field Value

StyledProperty<bool>

ShowInTaskbarProperty

Enables or disables the taskbar icon

public static readonly StyledProperty<bool> ShowInTaskbarProperty

Field Value

StyledProperty<bool>

SizeToContentProperty

Defines the SizeToContent property.

public static readonly StyledProperty<SizeToContent> SizeToContentProperty

Field Value

StyledProperty<SizeToContent>

SystemDecorationsProperty

Defines the SystemDecorations property.

public static readonly StyledProperty<SystemDecorations> SystemDecorationsProperty

Field Value

StyledProperty<SystemDecorations>

TitleProperty

Defines the Title property.

public static readonly StyledProperty<string?> TitleProperty

Field Value

StyledProperty<string>

WindowClosedEvent

Routed event that can be used for global tracking of window destruction

public static readonly RoutedEvent<RoutedEventArgs> WindowClosedEvent

Field Value

RoutedEvent<RoutedEventArgs>

WindowDecorationMarginProperty

Defines the WindowDecorationMargin property.

public static readonly DirectProperty<Window, Thickness> WindowDecorationMarginProperty

Field Value

DirectProperty<Window, Thickness>

WindowOpenedEvent

Routed event that can be used for global tracking of opening windows

public static readonly RoutedEvent<RoutedEventArgs> WindowOpenedEvent

Field Value

RoutedEvent<RoutedEventArgs>

WindowStartupLocationProperty

Defines the WindowStartupLocation property.

public static readonly StyledProperty<WindowStartupLocation> WindowStartupLocationProperty

Field Value

StyledProperty<WindowStartupLocation>

WindowStateProperty

Represents the current window state (normal, minimized, maximized)

public static readonly StyledProperty<WindowState> WindowStateProperty

Field Value

StyledProperty<WindowState>

Properties

CanResize

Enables or disables resizing of the window.

public bool CanResize { get; set; }

Property Value

bool

ClosingBehavior

Gets or sets a value indicating how the Closing event behaves in the presence of child windows.

public WindowClosingBehavior ClosingBehavior { get; set; }

Property Value

WindowClosingBehavior

ExtendClientAreaChromeHints

Gets or Sets the ExtendClientAreaChromeHints that control how the chrome looks when the client area is extended.

public ExtendClientAreaChromeHints ExtendClientAreaChromeHints { get; set; }

Property Value

ExtendClientAreaChromeHints

ExtendClientAreaTitleBarHeightHint

Gets or Sets the TitlebarHeightHint for when the client area is extended. A value of -1 will cause the titlebar to be auto sized to the OS default. Any other positive value will cause the titlebar to assume that height.

public double ExtendClientAreaTitleBarHeightHint { get; set; }

Property Value

double

ExtendClientAreaToDecorationsHint

Gets or sets if the ClientArea is Extended into the Window Decorations (chrome or border).

public bool ExtendClientAreaToDecorationsHint { get; set; }

Property Value

bool

Icon

Gets or sets the icon of the window.

public WindowIcon? Icon { get; set; }

Property Value

WindowIcon

IsExtendedIntoWindowDecorations

Gets if the ClientArea is Extended into the Window Decorations.

public bool IsExtendedIntoWindowDecorations { get; }

Property Value

bool

OffScreenMargin

Gets the window margin that is hidden off the screen area. This is generally only the case on Windows when in Maximized where the window border is hidden off the screen. This Margin may be used to ensure user content doesnt overlap this space.

public Thickness OffScreenMargin { get; }

Property Value

Thickness

OwnedWindows

Gets a collection of child windows owned by this window.

public IReadOnlyList<Window> OwnedWindows { get; }

Property Value

IReadOnlyList<Window>

PlatformImpl

Gets the platform-specific window implementation.

public IWindowImpl? PlatformImpl { get; }

Property Value

IWindowImpl

Position

Gets or sets the window position in screen coordinates.

public PixelPoint Position { get; set; }

Property Value

PixelPoint

ShowActivated

Gets or sets a value that indicates whether a window is activated when first shown.

public bool ShowActivated { get; set; }

Property Value

bool

ShowInTaskbar

Enables or disables the taskbar icon

public bool ShowInTaskbar { get; set; }

Property Value

bool

SizeToContent

Gets or sets a value indicating how the window will size itself to fit its content.

public SizeToContent SizeToContent { get; set; }

Property Value

SizeToContent

Remarks

If SizeToContent has a value other than Manual, SizeToContent is automatically set to Manual if a user resizes the window by using the resize grip or dragging the border.

NOTE: Because of a limitation of X11, SizeToContent will be reset on X11 to Manual on any resize - including the resize that happens when the window is first shown. This is because X11 resize notifications are asynchronous and there is no way to know whether a resize came from the user or the layout system. To avoid this, consider setting CanResize to false, which will disable user resizing of the window.

StyleKeyOverride

Gets the type by which the element is styled.

protected override Type StyleKeyOverride { get; }

Property Value

Type

Remarks

Usually controls are styled by their own type, but there are instances where you want an element to be styled by its base type, e.g. creating SpecialButton that derives from Button and adds extra functionality but is still styled as a regular Button. Override this property to change the style for a control class, returning the type that you wish the elements to be styled as.

SystemDecorations

Sets the system decorations (title bar, border, etc)

public SystemDecorations SystemDecorations { get; set; }

Property Value

SystemDecorations

Title

Gets or sets the title of the window.

public string? Title { get; set; }

Property Value

string

WindowDecorationMargin

Gets the WindowDecorationMargin. This tells you the thickness around the window that is used by borders and the titlebar.

public Thickness WindowDecorationMargin { get; }

Property Value

Thickness

WindowStartupLocation

Gets or sets the startup location of the window.

public WindowStartupLocation WindowStartupLocation { get; set; }

Property Value

WindowStartupLocation

WindowState

Gets or sets the minimized/maximized state of the window.

public WindowState WindowState { get; set; }

Property Value

WindowState

Methods

ArrangeSetBounds(Size)

Called during the arrange pass to set the size of the window.

protected override sealed Size ArrangeSetBounds(Size size)

Parameters

size Size

The requested size of the window.

Returns

Size

The actual size of the window.

BeginMoveDrag(PointerPressedEventArgs)

Starts moving a window with left button being held. Should be called from left mouse button press event handler

public void BeginMoveDrag(PointerPressedEventArgs e)

Parameters

e PointerPressedEventArgs

BeginResizeDrag(WindowEdge, PointerPressedEventArgs)

Starts resizing a window. This function is used if an application has window resizing controls. Should be called from left mouse button press event handler

public void BeginResizeDrag(WindowEdge edge, PointerPressedEventArgs e)

Parameters

edge WindowEdge
e PointerPressedEventArgs

Close()

Closes the window.

public void Close()

Close(object?)

Closes a dialog window with the specified result.

public void Close(object? dialogResult)

Parameters

dialogResult object

The dialog result.

Remarks

When the window is shown with the ShowDialog<TResult>(Window) or ShowDialog<TResult>(Window) method, the resulting task will produce the Avalonia.Controls.Window._dialogResult value when the window is closed.

ExtendClientAreaToDecorationsChanged(bool)

protected virtual void ExtendClientAreaToDecorationsChanged(bool isExtended)

Parameters

isExtended bool

Hide()

Hides the window but does not close it.

public override void Hide()

IsVisibleChanged(AvaloniaPropertyChangedEventArgs)

protected override void IsVisibleChanged(AvaloniaPropertyChangedEventArgs e)

Parameters

e AvaloniaPropertyChangedEventArgs

MeasureOverride(Size)

Measures the control and its child elements as part of a layout pass.

protected override Size MeasureOverride(Size availableSize)

Parameters

availableSize Size

The size available to the control.

Returns

Size

The desired size for the control.

OnClosing(WindowClosingEventArgs)

Raises the Closing event.

protected virtual void OnClosing(WindowClosingEventArgs e)

Parameters

e WindowClosingEventArgs

The event args.

Remarks

A type that derives from Window may override OnClosing(WindowClosingEventArgs). The overridden method must call OnClosing(WindowClosingEventArgs) on the base class if the Closing event needs to be raised.

OnCreateAutomationPeer()

Returns a new, type-specific AutomationPeer implementation for the control.

protected override AutomationPeer OnCreateAutomationPeer()

Returns

AutomationPeer

The type-specific AutomationPeer implementation.

OnPropertyChanged(AvaloniaPropertyChangedEventArgs)

Called when a avalonia property changes on the object.

protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)

Parameters

change AvaloniaPropertyChangedEventArgs

The property change details.

Show()

Shows the window.

public override void Show()

Exceptions

InvalidOperationException

The window has already been closed.

Show(Window)

Shows the window as a child of owner.

public void Show(Window owner)

Parameters

owner Window

Window that will be the owner of the shown window.

Exceptions

InvalidOperationException

The window has already been closed.

ShowDialog(Window)

Shows the window as a dialog.

public Task ShowDialog(Window owner)

Parameters

owner Window

The dialog's owner window.

Returns

Task

A task that can be used to track the lifetime of the dialog.

Exceptions

InvalidOperationException

The window has already been closed.

ShowDialog<TResult>(Window)

Shows the window as a dialog.

public Task<TResult> ShowDialog<TResult>(Window owner)

Parameters

owner Window

The dialog's owner window.

Returns

Task<TResult>

. A task that can be used to retrieve the result of the dialog when it closes.

Type Parameters

TResult

The type of the result produced by the dialog.

SortWindowsByZOrder(Window[])

Sorts the windows ascending by their Z order - the topmost window will be the last in the list.

public static void SortWindowsByZOrder(Window[] windows)

Parameters

windows Window[]

Events

Closing

Fired before a window is closed.

public event EventHandler<WindowClosingEventArgs>? Closing

Event Type

EventHandler<WindowClosingEventArgs>