WPF窗口位置 [英] WPF window position

查看:375
本文介绍了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?

推荐答案

解决方案工作对我罚款。

This solution worked fine for me.

下面是一个方法,我已经找到了为中心的一个窗口要么其父母或主窗口的应用程序,在WPF。这不是从你怎么做的WinForms的太不一样了。

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.

有关的子窗口,设置它的WindowStartupLocation为CenterOwner。这将导致其在所属窗口的中心,以显示。 折叠

For the child window, set its WindowStartupLocation to "CenterOwner". This will cause it to show in the center of the owning Window. Collapse

<Window x:Class="WpfApplication1.TestChild"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TestChild" Height="300" Width="300"
    WindowStartupLocation="CenterOwner">

现在,所有剩下来要做的显示之前设置它的主人。如果code你使用显示窗口一个窗口类的内部运行,那么你可以只用这个。 折叠

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

TestChild testWindow = new TestChild();
testWindow.Owner = this;
testWindow.Show();

这并非总是如此,但是,有时候,你需要显示在code运行的子窗口页面或用户控件上。在这种情况下,要在子窗口为中心的应用程序的主窗口。 折叠

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

TestChild testWindow = new TestChild();
testWindow.Owner = Application.Current.MainWindow;
testWindow.Show();

这篇关于WPF窗口位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆