将一个 WPF Window 并排放置

Place one WPF Window next to another

我用过这个:

WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;

将第一个 window (W1) 放在屏幕中间。

单击按钮,我想在第一个旁边放置一个新的 window (W2)。

Image showing how it should be (W=Window)

下面的代码应该可以完成工作:

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

如果您希望第二个 window 跟踪第一个的大小和位置的变化,那么您需要处理适当的事件并使用类似的方法更正第二个 window 的位置以上逻辑。