【问题标题】:Set WPF Dialog Owner from a Winforms ElementHost WPF UserControl从 Winforms ElementHost WPF UserControl 设置 WPF 对话框所有者
【发布时间】:2016-04-14 02:45:48
【问题描述】:

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

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

【问题讨论】:

    标签: c# wpf winforms


    【解决方案1】:

    获取所有者集的诀窍是访问底层 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"
    

    【讨论】:

      猜你喜欢
      • 2013-08-20
      • 2015-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多