c# - 从 Winforms ElementHost WPF UserControl 设置 WPF 对话框所有者

标签 c# wpf winforms

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

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

最佳答案

获取所有者集的技巧是访问底层 WinForms 窗口并使用 WPF WindowInteropHelper 将其全部组合在一起。

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,您还需要:

using System.Windows.Interop

此 XAML 减少了任务栏的困惑

ShowInTaskbar="False"

关于c# - 从 Winforms ElementHost WPF UserControl 设置 WPF 对话框所有者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36606973/

相关文章:

c# - 为依赖属性创建代理

c# - RelayCommand CanExecute 行为

c# - 在 NUnit 2.5.8 中使用 TestContext 时出现 NullReferenceException

c# - 将 c#/WPF GUI 围绕 c++/cli 围绕 native c++ 包装

c# - 执行未知类型的 DynamicExpression

c# - WinForms 图表 : Set minimum Y Axis display range

vb.net - 适用于 .NET 的免费时间表/时间表 GUI 库

vb.net - 检测(右键)单击标签页标题

c# - 在这种特定且非常简单的情况下,为什么 Debug 不同于 Release?

c# - 通过 Windows 窗体上的单个按钮将数据多次插入到 SQL 表中