那么这个问题和这个问题是相似的,但没有答案可行.事实上,我希望WindowStartupLocation = CenterOwner可以工作......但事实并非如此.它似乎将新窗口置于网格列的中心,而不是主窗口的中心.所以我假设它认为那是父母.第二,当我关闭对话框并再次打开它时,它不会居中,而是从前一个位置向下移动.如果我将主窗口移动到第二个监视器,弹出窗口仍会在默认监视器上打开.这些属性是错误的还是我认为它应该以不同的方式工作.我想我可以手动计算Top和Left属性.我只是希望弹出窗口在主窗口中居中,无论它在哪里.
Car*_*rlo 78
可能是因为您没有设置所有者:
this.Owner = App.MainWindow; // for example
Run Code Online (Sandbox Code Playgroud)
这就是我如何做到这一点,它始终将窗口完美地集中在一起.
要扩展Will Eddins所评论的内容,您可以在Window中为ShowDialog()或Show()创建一个重载方法:
public void ShowDialog(Window owner)
{
this.Owner = owner;
this.ShowDialog();
}
public void Show(Window owner)
{
this.Owner = owner;
this.Show();
}
Run Code Online (Sandbox Code Playgroud)
或者重载构造函数:
public MyWindow(Window owner)
: this()
{
this.Owner = owner;
}
Run Code Online (Sandbox Code Playgroud)
如果你为此创建一个扩展,你可以重用这个好主意:
/// <summary>
/// Opens a window modally, with an owner
/// </summary>
/// <param name="window">The window to open</param>
/// <param name="opener">The owner of the window getting opened</param>
/// <returns>window.ShowDialog()</returns>
public static bool? ShowDialog(this Window window, Window opener)
{
window.Owner = opener;
return window.ShowDialog();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29090 次 |
| 最近记录: |