WPF – 设置对话窗口相对于主窗口的位置?

我只是创建我自己的AboutBox,我使用Window.ShowDialog()

如何让它相对于主窗口的位置,即从顶部开始为20px并居中。

谢谢。

解决方法

您可以简单地使用 Window.LeftWindow.Top属性。从主窗口读取它们,并在调用ShowDialog()方法之前将值(加上20像素或其他)分配给AboutBox。
AboutBox dialog = new AboutBox();
dialog.Top = mainWindow.Top + 20;

要让它居中,您也可以使用WindowStartupLocation属性。设置为WindowStartupLocation.CenterOwner

AboutBox dialog = new AboutBox();
dialog.Owner = Application.Current.MainWindow; // We must also set the owner for this to work.
dialog.WindowStartupLocation = WindowStartupLocation.CenterOwner;

如果您希望将其水平居中,但不是垂直居中(即固定的垂直位置),则在加载关于框之后,您必须在EventHandler中执行此操作,因为您需要根据AboutBox的宽度来计算水平位置,这仅在加载后才知道。

protected override void OnInitialized(...)
{
    this.Left = this.Owner.Left + (this.Owner.Width - this.ActualWidth) / 2;
    this.Top = this.Owner.Top + 20;
}

gehho。

相关文章

Css常用的排序方式权重分配 排序方式: 1、按类型 ...
原文:https://www.cnblogs.com/wenruo/p/9732704.html 先上...
css属性:word-wrap:break-word; 与 word-break:break-all 的...
https://destiny001.gitee.io/color/
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML...
css之background的cover和contain的缩放背景图 对于这两个属...