从 Winforms ElementHost WPF UserControl 设置 WPF 对话框所有者 [英] Set WPF Dialog Owner from a Winforms ElementHost WPF UserControl

查看:41
本文介绍了从 Winforms ElementHost WPF UserControl 设置 WPF 对话框所有者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WinForms 应用程序,我在 ElementHost 控件中托管了一个 WPF 用户控件.从那个 WPF UserControl 我需要显示一个 WPF 对话框.虽然我可以创建 WPF 窗口并调用 ShowDialog(),但我可以让对话框隐藏在"主应用程序后面.如何在此上下文中设置 WPF 对话框的所有者?

I have a WinForms application where I'm hosting a WPF user control in an ElementHost control. From that WPF UserControl I need to show a WPF dialog. While I could create the WPF Window and call ShowDialog(), I could get the dialog to "hide behind" the main app. How can I set the owner of the WPF dialog in this context?

EntryDialog entryDialog = new entryDialog();
bool? ret = entryDialog.ShowDialog();
if (ret.Value == true)
{
}

推荐答案

设置所有者的技巧是访问底层的 WinForms 窗口并使用 WPF WindowInteropHelper 将其全部拉到一起.

The trick to getting the owner set was to access the underlying WinForms window and use the WPF WindowInteropHelper to pull it all together.

EntryDialog entryDialog = new entryDialog();
HwndSource source = (HwndSource)HwndSource.FromVisual(this);
new System.Windows.Interop.WindowInteropHelper(entryDialog).Owner = source.Handle;
bool? ret = entryDialog.ShowDialog();
if (ret.Value == true)
{
}

对于 HwndSource,您还需要:

For the HwndSource you also need:

using System.Windows.Interop

这个 XAML 减少了任务栏的混乱

This XAML reduces the task bar clutter

ShowInTaskbar="False"

这篇关于从 Winforms ElementHost WPF UserControl 设置 WPF 对话框所有者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆