WPF 窗口位置
我之前曾在此处问过有关创建子窗口的问题...现在当我打开子窗口时,它不会以父窗口为中心打开。如何将其设置为以父窗口为中心打开?
I have asked a question before about creating a child window here ... Now when I open child window, it doesn`t open centered to the parent window. How can I set it to open centered to the parent window?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个解决方案对我来说效果很好。
下面是我在 WPF 中发现的一种方法,用于将窗口居中于其父窗口或应用程序的主窗口。这与在 WinForms 中的操作方式没有太大不同。
对于子窗口,将其 WindowStartupLocation 设置为“CenterOwner”。这将导致它显示在所属窗口的中心。
。
现在,剩下要做的就是在显示它之前设置它的所有者 如果您用来显示窗口的代码在 Window 类内部运行,那么您可以使用它。
然而
,情况并非总是如此。有时,您需要从页面或用户控件上运行的代码显示子窗口。在这种情况下,您希望子窗口以应用程序的主窗口为中心。
坍塌
This solution worked fine for me.
Here’s a method I’ve found for centering a window to either its parent or the main window for the application, in WPF. It’s not too different from how you do it in WinForms.
For the child window, set its WindowStartupLocation to “CenterOwner”. This will cause it to show in the center of the owning Window.
Collapse
Now, all that’s left to do is set its owner before displaying it. If the code you’re using to display the window is running inside of a Window class, then you can just use this.
Collapse
This isn’t always the case, however; sometimes, you need to display the child window from the code running on a page or a user control. In this case, you want the child window to be centered to the main window of the application.
Collapse
尝试这个。
Try this.
你可以试试这个:
You can try this: