Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

wpf

I just want to set windowstartuplocation to top right corner of desktop. Now to clear some things up, I saw this thread with same question:

Changing the start up location of a WPF window

Now I don't want to get confused to what they are referring as right or left, 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?

like image 959
vish213 Avatar asked Sep 25 '12 20:09

vish213


2 Answers

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.

like image 172
Reed Copsey Avatar answered Nov 09 '22 21:11

Reed Copsey


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" >
like image 1
Josh Avatar answered Nov 09 '22 23:11

Josh