Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to open a WPF dialog from a WinForms menu

Tags:

winforms

wpf

I have a WinForms menuitem and I need that on menu click the new WPF dialog is loaded. How do I do this? Thanks

like image 275
Elena Avatar asked Dec 22 '22 23:12

Elena


1 Answers

You should be able to display it by creating a new instance of the WPF class for the dialog, and then calling its ShowDialog() method.

The only trick is properly setting the owner of the WPF dialog. You can't just set the Owner property directly, since that requires a WPF window. However, you can use the class System.Windows.Interop.WindowInterpHelper to get around this:

MyWpfDialog dialog = new MyWpfDialog();
new System.Windows.Interop.WindowInteropHelper(dialog).Owner = this.Handle;
dialog.ShowDialog();

(I got the code sample from http://blog.stpworks.com/archive/2009/07/02/setting-wpf-dialog-owner-from-within-winforms-application.aspx.)

like image 51
Andy Avatar answered Jan 04 '23 20:01

Andy