Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to offset the window position by a value when WindowStartupLocation="CenterScreen"?

Tags:

c#

wpf

The requirement used to be that the window is CenterScreen but the client has recently asked that the window is now slightly to the right of its current position.

The current definition of the window is:

I was hoping that doing the following would work:

    public MyWindow()
    {
        InitializeComponent();

        // change the position after the component is initialized
        this.Left = this.Left + this.Width;
    }

But the window is still in the same position when it appears on screen.

Do I need to change the startup location to Manual and position it myself? If so, how do I get it offset from the center?

like image 264
DaveDev Avatar asked Nov 01 '13 11:11

DaveDev


People also ask

What is the difference between centerowner and windowstartuplocation?

Setting the WindowStartupLocation property to CenterScreen causes a window to be positioned in the center of the screen that contains the mouse cursor. Setting the WindowStartupLocation property to CenterOwner causes a window to be positioned in the center of its owner window (see Window.Owner), if specified.

What is the use of windowstartuplocation?

A WindowStartupLocation value that specifies the top/left position of a window when first shown. The default is Manual. Setting the WindowStartupLocation property to Manual causes a window to be positioned according to its Left and Top property values. If either the Left or Top properties aren't specified, their values are determined by Windows.

What is Windows system window startup location?

System. Windows Window. Window Startup Location Property System. Windows Gets or sets the position of the window when first shown. A WindowStartupLocation value that specifies the top/left position of a window when first shown. The default is Manual.

Can I set the value of this property when a window?

You cannot set or get the value of this property when a window is hosted in a browser. Gets or sets a value that indicates whether a window will automatically size itself to fit the size of its content. Occurs when a mouse button is clicked two or more times.


1 Answers

Handle Loaded event, and put this.Left = this.Left + this.Width; there:

public MyWindow()
    {
        InitializeComponent();

        this.Loaded += new RoutedEventHandler(MyWindow_Loaded);
    }


 void MyWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.Left = this.Left + this.Width;
        }
like image 151
Bolu Avatar answered Sep 28 '22 08:09

Bolu