Changing the start up location of a WPF window

WpfWindowWpf Positioning

Wpf Problem Overview


I'd like to have a WPF window open in the top right part of the screen.

Right now I can achieve that by opening the window and then moving it (via movewindow in user32.dll). However, this approach means the window opens in it's default location, fully loads, and then moves to the top right.

How could I do I change it so that I could specify the window's initial position and size?

Wpf Solutions


Solution 1 - Wpf

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>

Solution 2 - Wpf

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;

Solution 3 - Wpf

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"
>
...

Solution 4 - Wpf

There is a property for Window, called "WindowStartupLocation" You can find that in properties window. Simply just select Window in constructor, then go to properties list. Search for "Startup" or smth similar and you can find that property. Change it to "CenterScreen" and it will make the deal. NOTE! Make sure, that you did not select grid instead of window! Otherwise you`ll fail.

Or you just can done it via XAML editing as some guys wrote before.

Solution 5 - Wpf

This is what worked for me (with a different placement on screen):

<Window x:Class="BtnConfig.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:BtnConfig"
        mc:Ignorable="d"
        Title="MainWindow" Height="142.802" Width="448.089"
        Top="288" Left="0"> 
</Window>

Notice it does not contain:

WindowStartupLocation="Manual" 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEvanView Question on Stackoverflow
Solution 1 - WpfReed CopseyView Answer on Stackoverflow
Solution 2 - WpfChristian LarssonView Answer on Stackoverflow
Solution 3 - WpfnoelicusView Answer on Stackoverflow
Solution 4 - WpfАндрей РыжовView Answer on Stackoverflow
Solution 5 - WpfMichael WebbView Answer on Stackoverflow