【问题标题】:Place one WPF Window next to another将一个 WPF 窗口放在另一个旁边
【发布时间】:2016-05-25 13:25:19
【问题描述】:

我用过这个:

WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;

将第一个窗口 (W1) 定位在屏幕中间。

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

Image showing how it should be (W=Window)

【问题讨论】:

  • 快速浏览一下,这似乎只是基本计算加减的问题。尝试获取 firstWindow 的位置值和宽度、高度。(通过 firstWindow.Width 和 firstWindow.Height)。并经过简单计算,将计算值设置为 secondWindow.Left 和 secondWindow.Top 之类的。

标签: c# wpf window location


【解决方案1】:

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

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

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

【讨论】:

  • 您可能希望使用 ActualWidth 和 ActualHeight 来说明 DPI 调整和测量。
猜你喜欢
  • 1970-01-01
  • 2010-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多