WPF ShowDialog 和 ElementHost

发布于 2024-08-04 11:02:09 字数 327 浏览 9 评论 0原文

是否可以从作为 ElementHost 子级的 WPF 用户控件显示模态窗口,并将模态窗口的所有者/父级设置为包含的 Form 控件?

我猜您不能执行此操作,因为 Owner 属性采用 Window 的实例,而我想将其设置为 Element Host 控件的父级,该控件是一个旧的 Windows Forms Form 控件。只是想知道是否有解决方法或替代方法。

问题是,当显示模态窗口并且用户切换到另一个应用程序,然后再次返回时,模态窗口将被隐藏,并且用户无法与主窗口交互。这是因为 Windows 认为模态窗口仍然显示,但实际上并没有显示,因为没有设置所有者/父级关系。

干杯, 詹姆斯.

is it possible to display a Modal Window from a WPF User Control, that is a child of an ElementHost, and set the owner/parent of the Modal Window to the containing Form control?

I'm guessing you can't do this, as the Owner property takes an instance of Window, where as I want to set it to the parent of the Element Host control, which is an old Windows Forms Form control. Just wondering if there is a work around or alternative approach.

The problem is when the Modal Window is displayed and the user switches to another application, then back again, the Modal Window is hidden and the user is unable to interact with the main Window. This is due to Windows thinking the Modal Window is still displayed, when it isn't, as there is no Owner/Parent relationship set.

Cheers,
James.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

找个人就嫁了吧 2024-08-11 11:02:09

我正在使用 WindowInteropHelper 来解决这个问题,如下所示:

var wpfDialog = new MyWpfDialog();
var interopHelper = new WindowInteropHelper(wpfDialog)  
        {
            Owner = winFormsDialog.Handle
        };

wpfDialog.ShowDialog();

I'm using WindowInteropHelper to solve that problem like this:

var wpfDialog = new MyWpfDialog();
var interopHelper = new WindowInteropHelper(wpfDialog)  
        {
            Owner = winFormsDialog.Handle
        };

wpfDialog.ShowDialog();
一瞬间的火花 2024-08-11 11:02:09

我知道这篇文章很旧,但我遇到了一种从 wpf UserControl 的上下文中查找托管 ElementHost 的 winform 窗口的方法,在该上下文中您可能无法访问 winform 窗口。我发现这很有用,这样我就不必传递主机窗口了。


HwndSource winformWindow = (System.Windows.Interop.HwndSource.FromDependencyObject(wpfControlInElementHost) as System.Windows.Interop.HwndSource);
if (winformWindow != null)
{
   var interopHelper = new WindowInteropHelper(wpfWindow)
   {
      Owner = winformWindow.Handle
   };
}

I know this post is old, but I came across a way to find the winform window that is hosting the ElementHost from the context of a wpf UserControl where you may not have access to the winform window. I found this to be useful so that I don't have to pass the host window around.


HwndSource winformWindow = (System.Windows.Interop.HwndSource.FromDependencyObject(wpfControlInElementHost) as System.Windows.Interop.HwndSource);
if (winformWindow != null)
{
   var interopHelper = new WindowInteropHelper(wpfWindow)
   {
      Owner = winformWindow.Handle
   };
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文