Changing the start up location of a WPF window Changing the start up location of a WPF window wpf wpf

Changing the start up location of a WPF window


Just set WindowStartupLocation, Height, Width, Left, and Top in xaml:

<Window x:Class="WpfApplication1.Window1"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     Title="Window1"     Height="500" Width="500"    WindowStartupLocation="Manual"     Left="0" Top="0"></Window>


For people who like me wanted to set the position of the window to the current mouse position, you can do it like this:

myWindow.WindowStartupLocation = WindowStartupLocation.Manual;myWindow.Left = PointToScreen(Mouse.GetPosition(null)).X;myWindow.Top = PointToScreen(Mouse.GetPosition(null)).Y;


I like to use WindowStartupLocation="CenterOwner" (MSDN docs for it)

The caller needs to specify itself as owner for this to work though, such as:

new MyWindow() { Owner = this }.ShowDialog();

Then just define the window height and width, e.g:

<Window ...     Height="400" Width="600"     WindowStartupLocation="CenterOwner">...