简体   繁体   English

单击主应用程序 window 时,如何聚焦模态 WPF Window

[英]How do I focus a modal WPF Window when the main application window is clicked

I have my MainApplication Window that launches a new Window with.ShowDialog() so that it is modal.我有我的 MainApplication Window 启动一个新的 Window with.ShowDialog() 以便它是模态的。

UploadWindow uploadWindow = new UploadWindow();
uploadWindow.ShowDialog();

Now users are often leaving this window open and it can get lost under other windows. When the MainApplication is clicked you get an error-like beep and are unable to interact with it, so the modal window is blocking properly as expected, but it would be nice if the modal window was focused at this point to show the user it was still open.现在用户经常让这个 window 打开,它可能会在其他 windows 下迷路。单击 MainApplication 时,您会收到类似错误的蜂鸣声并且无法与其交互,因此模态 window 按预期正确阻止,但它会如果模式 window 在这一点上被聚焦以向用户显示它仍然是打开的,那就太好了。

Currently it just looks as if the MainApplication window has locked up.目前看起来好像 MainApplication window 已经锁定。

Try setting the dialog's owner:尝试设置对话框的所有者:

var uploadWindow = new UploadWindow();
uploadWindow.Owner = this;
uploadWindow.ShowDialog();

I have the problem, that I can't use this, if someone have the same problem, you can use我有问题,我不能用这个,如果有人有同样的问题,你可以使用

Window.GetWindow(this)

Since I am using MVVM, I'm not creating the code from the GUI.因为我使用的是 MVVM,所以我没有从 GUI 创建代码。 I used this.我用过这个。

var uploadWindow = new UploadWindow();
uploadWindow.Owner = Application.Current.MainWindow;
uploadWindow.ShowDialog();

If all of the above solutions tried and still facing the same problem then here is your tested and verified solution go to your window xaml and add如果以上所有解决方案都尝试过但仍然面临同样的问题,那么这里是您经过测试和验证的解决方案 go 到您的 window xaml 并添加

ResizeMode = "NoResize" ResizeMode = "NoResize"

Nowadays you can just set Topmost = true现在你可以设置Topmost = true

var uploadWindow = new UploadWindow();
uploadWindow.Topmost = true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM