从 Winforms ElementHost WPF UserControl 设置 WPF 对话框所有者 [英] Set WPF Dialog Owner from a Winforms ElementHost WPF UserControl
问题描述
我有一个 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屋!