Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a WinForms Form as Owner for a WPF Window

I have a main application which is built around a System.Windows.Forms.Form There is a legacy System.Windows.Window that the user can call up which is set as Topmost. I would like the Window to be owned by the main application, so it will minimize when the application is minimized. So my expected code was

TopmostDisplayWindow.Owner = MainAppForm;

However, there is no Window.Owner method that takes a Form and Window has no override of Show() that takes the owner.

Is there an easy way to get a Window for the MainAppForm or would it take something more complicated?

like image 960
Dragonel Avatar asked Sep 21 '15 17:09

Dragonel


People also ask

Can you mix WPF and WinForms?

Yes you can, both Windows Forms within a WPF application, and WPF controls within Windows Forms.


1 Answers

You can use WindowInteropHelper and set Owner property of it:

var window = new YourWPFWindow();
WindowInteropHelper helper = new WindowInteropHelper(window);
helper.Owner = this.Handle;
window.Show();

Use the above code in your main/parent form which is a Winform Form. Remember to add following references when you want to show your WPF Window:

  1. PresentationCore
  2. PresentationFramework
  3. WindowsBase
like image 87
Reza Aghaei Avatar answered Oct 17 '22 20:10

Reza Aghaei