DaveDev
DaveDev

Reputation: 42225

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

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?

Upvotes: 6

Views: 1135

Answers (2)

hungrycoder
hungrycoder

Reputation: 507

In the designer right click on the window select Properties. in the properties side panel look for "StartPosition" Set its value to "CenterScreen".

Or You can try the follwoing code in InitializeComponent() of the window

this.StartPosition=FormStartPosition.CenterScreen;

Or if it is a popup window, then try using the below code.

this.StartPosition=FormStartPosition.CenterParent;

Or if it is the window from which the application starts with then use the follwoing setting.

this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen

Upvotes: -1

Bolu
Bolu

Reputation: 8786

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;
        }

Upvotes: 6

Related Questions