Search code examples
c#wpfwindowlocation

Place one WPF Window next to another


I used this:

WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;

to position the first window (W1) in the middle of the screen.

With a button click, I want to place a new window (W2) beside the first one.

Image showing how it should be (W=Window)


Solution

  • The following code should do the job :

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Window2 w2 = new Window2();
        w2.WindowStartupLocation = WindowStartupLocation.Manual;
        w2.Left = this.Left + this.Width;
        w2.Top = this.Top + (this.Height - w2.Height) / 2;
        w2.Show();
    }
    

    If you want the second window to track changes to the size and position of the first then you'd need to handle the appropriate events and correct the position of the second window using similar logic to the above.