John Dyer
John Dyer

Reputation: 2358

Set WPF Dialog Owner from a Winforms ElementHost WPF UserControl

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)
{
}

Upvotes: 2

Views: 1323

Answers (1)

John Dyer
John Dyer

Reputation: 2358

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)
{
}

For the HwndSource you also need:

using System.Windows.Interop

This XAML reduces the task bar clutter

ShowInTaskbar="False"

Upvotes: 2

Related Questions