vish213
vish213

Reputation: 786

How to set WPF application windowstartuplocation to top right corner using xaml?

I just want to set windowstartuplocation to top right corner of desktop. I saw this thread with same question:

Changing the start up location of a WPF window

I want my application to start in top right corner,where right refers to MY RIGHT SIDE(not as if my desktop is a person looking at me and ITS RIGHT SIDE).So,

1.) Setting left and top to 0 only is not a solution(brings app to left side not right)

2.) I tried using SystemParameters.PrimaryScreenWidth, but I can't perform operation to subtract the width of my app from this value at binding time.

Is there a way I can do it without going into much complexity?

Upvotes: 15

Views: 30774

Answers (2)

Josh
Josh

Reputation: 79

By using WorkArea.Width make sure the width of the taskbar is taken into account (when placed on a side, left or right). By using this.Width or this.MaxWidth make sure your window never slips outside the work area, but both require their value be set (in xaml or in code behind) to not produce the value NaN (Not a Number) for this.Left.

public MainWindow()
    {
        InitializeComponent();
        this.Left = SystemParameters.WorkArea.Width - this.MaxWidth;
    }

<Window
    ... more code here ...
    WindowStartupLocation="Manual" Top="0" MaxWidth="500" >

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564363

Is there a way I can do it without going into much complexity?

The simplest way would be to setup your start location manually, and then set the Left property in code behind:

<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" 
    Top="0">
</Window> 

In your code behind:

public Window1()
{
    InitializeComponent();
    this.Left = SystemParameters.PrimaryScreenWidth - this.Width;
}

This is one place where I feel the simplicity of doing it in code outweights any disadvantages of introducing code behind.

Upvotes: 27

Related Questions