The inverse of this question has been answered a number of times.
public static void SetOwner(object activeWindow, object dialog)
{
if (IsWindow(dialog) && IsWindow(activeWindow))
{
(dialog as Window).Owner = (activeWindow as Window);
}
else if (IsForm(dialog) && IsForm(activeWindow))
{
(dialog as Form).Owner = (activeWindow as Form);
}
else if (IsWindow(dialog) && IsForm(activeWindow))
{
var wih = new WindowInteropHelper(dialog as Window);
wih.Owner = (activeWindow as Form).Handle;
}
else if (IsForm(dialog) && IsWindow(activeWindow开发者_开发技巧))
{
var dialogForm = dialog as Form;
var ownerWindow = activeWindow as Window;
// What goes here?
}
}
To expand upon SLaks' answer, here is an example such class:
public class WpfWindowWrapper : System.Windows.Forms.IWin32Window
{
public WpfWindowWrapper(System.Windows.Window wpfWindow)
{
Handle = new System.Windows.Interop.WindowInteropHelper(wpfWindow).Handle;
}
public IntPtr Handle { get; private set; }
}
It could be used like this from a WPF window's code-behind:
var winForm = new MyWinForm();
winForm.ShowDialog(new WpfWindowWrapper(this));
In this way the Winform dialog acts as a proper child of the WPF window, with the correct modal behavior.
You need to create a class that implements the WinForms IWin32Window
interface and returns the WPF window's handle (using new WindowInteropHelper(window).Handle
), then pass that to the form's ShowDialog
.
精彩评论