Skip to main content
A top-level window that serves as the main container for your application UI.

Namespace

Avalonia.Controls

Inheritance

ControlTemplatedControlTopLevelWindowBaseWindow

Properties

Title
string?
Gets or sets the title of the window. Default is “Window”.
Icon
WindowIcon?
Gets or sets the icon of the window.
WindowState
WindowState
Gets or sets the minimized/maximized state of the window. Values: Normal, Minimized, Maximized, FullScreen.
SizeToContent
SizeToContent
Gets or sets how the window will size itself to fit its content. Values: Manual (default), Width, Height, WidthAndHeight.
WindowStartupLocation
WindowStartupLocation
Gets or sets the startup location of the window. Values: Manual, CenterScreen, CenterOwner.
CanResize
bool
Gets or sets whether resizing of the window is enabled. Default is true.
CanMinimize
bool
Gets or sets whether minimizing the window is enabled. Default is true.
CanMaximize
bool
Gets or sets whether maximizing the window is enabled. Default is true. When CanResize is false, this is always false.
WindowDecorations
WindowDecorations
Gets or sets the window decorations (title bar, border, etc). Values: None, BorderOnly, Full (default).
ShowActivated
bool
Gets or sets whether a window is activated when first shown. Default is true.
ShowInTaskbar
bool
Gets or sets whether the window appears in the taskbar. Default is true.
ExtendClientAreaToDecorationsHint
bool
Gets or sets if the ClientArea is extended into the window decorations (chrome or border). Default is false.
ExtendClientAreaTitleBarHeightHint
double
Gets or sets the TitlebarHeightHint for when the client area is extended. -1 causes auto-sizing. Default is -1.
IsExtendedIntoWindowDecorations
bool
Gets if the ClientArea is extended into the window decorations. Read-only.
WindowDecorationMargin
Thickness
Gets the thickness around the window that is used by borders and the titlebar. Read-only.
ClosingBehavior
WindowClosingBehavior
Gets or sets how the Closing event behaves in the presence of child windows. Values: OwnerAndChildWindows, OwnerWindowOnly.
Position
PixelPoint
Gets or sets the window position in screen coordinates.
IsDialog
bool
Gets whether this window was opened as a dialog. Read-only.
OwnedWindows
IReadOnlyList<Window>
Gets a collection of child windows owned by this window. Read-only.

Events

Closing
EventHandler<WindowClosingEventArgs>
Fired before a window is closed. Can be cancelled by setting e.Cancel = true.

Routed Events

WindowOpened
RoutedEvent<RoutedEventArgs>
Routed event for global tracking of opening windows.
WindowClosed
RoutedEvent<RoutedEventArgs>
Routed event for global tracking of window destruction.

Methods

Show()
void
Shows the window.
Show(Window owner)
void
Shows the window as a child of the specified owner window.
ShowDialog(Window owner)
Task
Shows the window as a modal dialog.
ShowDialog<TResult>(Window owner)
Task<TResult>
Shows the window as a modal dialog and returns a result.
Close()
void
Closes the window.
Close(object? dialogResult)
void
Closes a dialog window with the specified result.
Hide()
void
Hides the window but does not close it.
BeginMoveDrag(PointerPressedEventArgs)
void
Starts moving a window with left button being held. Should be called from left mouse button press event handler.
BeginResizeDrag(WindowEdge, PointerPressedEventArgs)
void
Starts resizing a window. Should be called from left mouse button press event handler.

Protected Methods

OnClosing(WindowClosingEventArgs)
void
Raises the Closing event. Override to add custom logic before the window closes.

Usage

XAML Example - Basic Window

<Window xmlns="https://github.com/avaloniaui"
        Title="My Application"
        Width="800"
        Height="600"
        Icon="/Assets/icon.ico">
  <Grid>
    <!-- Your content here -->
  </Grid>
</Window>

XAML Example - Custom Window

<Window xmlns="https://github.com/avaloniaui"
        Title="Settings"
        Width="600"
        Height="400"
        WindowStartupLocation="CenterOwner"
        CanResize="False"
        ShowInTaskbar="False">
  <StackPanel Margin="20">
    <TextBlock Text="Settings" FontSize="24" />
    <!-- Settings content -->
  </StackPanel>
</Window>

C# Example - Creating and Showing Window

var window = new Window
{
    Title = "New Window",
    Width = 800,
    Height = 600,
    Content = new TextBlock { Text = "Hello, World!" }
};

window.Show();

C# Example - Modal Dialog

public async Task ShowSettingsDialog()
{
    var dialog = new SettingsWindow
    {
        Title = "Settings",
        Width = 500,
        Height = 400
    };
    
    await dialog.ShowDialog(this);
}

C# Example - Dialog with Result

public async Task<bool> ShowConfirmDialog()
{
    var dialog = new Window
    {
        Title = "Confirm",
        Width = 300,
        Height = 150,
        Content = new StackPanel
        {
            Children =
            {
                new TextBlock { Text = "Are you sure?", Margin = new Thickness(10) },
                new StackPanel
                {
                    Orientation = Orientation.Horizontal,
                    HorizontalAlignment = HorizontalAlignment.Right,
                    Children =
                    {
                        new Button 
                        { 
                            Content = "Yes",
                            Command = ReactiveCommand.Create(() => dialog.Close(true))
                        },
                        new Button 
                        { 
                            Content = "No",
                            Command = ReactiveCommand.Create(() => dialog.Close(false))
                        }
                    }
                }
            }
        }
    };
    
    return await dialog.ShowDialog<bool>(this);
}

Handling Window Closing

public class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        
        Closing += OnClosing;
    }
    
    private async void OnClosing(object? sender, WindowClosingEventArgs e)
    {
        // Cancel the close
        e.Cancel = true;
        
        // Show confirmation dialog
        var result = await ShowConfirmDialog();
        
        if (result)
        {
            // Allow close
            Closing -= OnClosing;
            Close();
        }
    }
}

Size to Content

var window = new Window
{
    Title = "Auto-sized Window",
    SizeToContent = SizeToContent.WidthAndHeight,
    Content = new StackPanel
    {
        Margin = new Thickness(20),
        Children =
        {
            new TextBlock { Text = "This window sizes to its content" },
            new Button { Content = "Click Me" }
        }
    }
};

Custom Title Bar

<Window ExtendClientAreaToDecorationsHint="True"
        ExtendClientAreaTitleBarHeightHint="40">
  <Panel>
    <!-- Custom title bar -->
    <StackPanel Height="40" 
                VerticalAlignment="Top"
                Background="#2196F3">
      <TextBlock Text="My App" 
                 Foreground="White"
                 Margin="10" />
    </StackPanel>
    
    <!-- Content -->
    <Grid Margin="0,40,0,0">
      <!-- Your content -->
    </Grid>
  </Panel>
</Window>

Enums

WindowState

Normal
The window is in its normal state.
Minimized
The window is minimized.
Maximized
The window is maximized.
FullScreen
The window is in full screen mode.

SizeToContent

Manual
The window will not automatically size itself to fit its content.
Width
The window will size itself horizontally to fit its content.
Height
The window will size itself vertically to fit its content.
WidthAndHeight
The window will size itself horizontally and vertically to fit its content.

WindowStartupLocation

Manual
The window position is determined by the Position property.
CenterScreen
The window is centered on the screen.
CenterOwner
The window is centered relative to its owner window.

Source File

Window.cs:1-1400+

See Also

Build docs developers (and LLMs) love