简体   繁体   English

作为WinForms工具窗口的WPF模式窗口消失

[英]WPF modal window as tool window in WinForms disappears

I was wondering if anyone can help me with this rather baffling situation. 我想知道是否有人可以帮助我解决这个相当令人困惑的情况。 I have a WPF form myForm which I am displaying modally in a WinForm application (without using an ElementHost). 我有一个WPF格式myForm ,我在WinForm应用程序中模态显示(不使用ElementHost)。 Everything works fine if I use the default WindowStyle and it is shown in the taskbar. 如果我使用默认的WindowStyle并且它显示在任务栏中,一切正常。 However now I don't want the form to show in the taskbar or contain the minimize button, therefore I have done the following: 但是现在我不希望表单显示在任务栏中或包含最小化按钮,因此我已经完成了以下操作:

MyForm myForm = new MyForm();
myForm.ShowInTaskbar = false;
myForm.WindowStyle = System.Windows.WindowStyle.ToolWindow;
myForm.WindowStartupLocation =System.Windows.WindowStartupLocation.CenterOwner;
myForm.ShowDialog();

Now, the wpf form displays as expected modally and without the minimize button. 现在,wpf表单按模式显示,没有最小化按钮。 If I now select the "parent" winform application in the taskbar, the wpf form disappears and there doesn't seem to be any way of returning to it! 如果我现在在任务栏中选择“父”winform应用程序,wpf表单就会消失,似乎没有任何方法可以返回它! I have read this which is similar but not the same (pure WPF application), so I can understand why the main app does not appear in the ALT+TAB menu, but can anyone tell me how I can return to the wpf form? 我读是相似但不相同的(纯WPF应用程序),所以我能理解为什么主要的应用程序不会在ALT + TAB菜单出现,但任何人都可以告诉我,我怎么能回到WPF形式?

Thanks in advance. 提前致谢。

The use of WindowsInteropHelper allows you to wrap a hosted WPF-form using a Winforms-form and set its parent as a Winforms control, which prevents the WPF form from disappearing (as also pointed out by dowhilefor and Hans Passant) 使用WindowsInteropHelper允许您使用Winforms-form包装托管的WPF表单并将其父级设置为Winforms控件,这可以防止WPF表单消失(如dowhilefor和Hans Passant所指出的那样)

Eg: 例如:

// Use the interop helper for the specified wpf window hosted by the win32 owner
WindowInteropHelper wih = new WindowInteropHelper(wpfForm);
wih.Owner = this.someWin32FormsControl.Handle;

This is not related to Winforms or WPF, this is the way Windows functions. 这与Winforms或WPF无关,这是Windows的运行方式。 The only solution that I think you have is to wire up your modality; 我认为你唯一的解决方案是连接你的模态; that is intercept the Activated event on your Winforms form and if your WPF tool window is visible, bring that window forward and give it focus. 即截取Winforms窗体上的Activated事件,如果您的WPF工具窗口可见,请将该窗口向前移动并使其聚焦。

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

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