我有两个显示器。当我的应用程序运行时,父级显示在第一个监视器上。当我将父窗口移动到第二个监视器并单击一个按钮(显示用于加载的 xaml 窗口)时,此子窗口保留在第一个监视器上。有没有办法让子窗口不管父窗口在什么位置都跟在父窗口里面?
请注意:父级是 winform ...子级是 xaml。
加载 xaml(子)
<Window x:Class="Test.Loading"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
WindowStyle="None"
AllowsTransparency="True"
WindowState="Maximized"
Background="Gray">
<Grid>
<Border>
<TextBlock Text="Loading ..." />
</Border>
</Grid>
</Window>
parent
private void btnShowLoading_Click(object sender, EventArgs e)
{
Loading load = new Loading();
double top = this.Top;
double left = this.Left;
load.Top = top;
load.Left = left;
load.Show();
}
最佳答案
试试这个:
childWindow.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
更新 1:
获取父窗口的位置并将其设置为子窗口。 如果您认为您的父窗口的状态发生了变化,您可以使用它来获取实际的顶部和左侧:
var leftField = typeof(Window).GetField("_actualLeft", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var topField = typeof(Window).GetField("_actualTop", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
double left = (double)leftField.GetValue(parentWindow);
double top = (double)topField.GetValue(parentWindow);
来源:Window ActualTop, ActualLeft
更新 2:
首先,您必须告知 parentWindow
是 WinForms
。
在这种情况下,您可以像这样获取 parentWindow 的 Top
和 Left
:
double top = this.Top;
double left = this.Left;
然后将parentWindow
的顶部和左侧传递给childWindow
。当 childWindow
加载时,它应该设置 top
和 left
。
关于c# - 双显示器父子窗口问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15550774/