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?
Yes you can, both Windows Forms within a WPF application, and WPF controls within Windows Forms.
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:
PresentationCore
PresentationFramework
WindowsBase
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With