Search code examples
c#wpfwinformsforms

Opening a new WPF form from another WPF form


Firstly, I have a project with a Windows Form that references another project with WPF forms. The Windows Form has an ElementHost which child is one of the WPF documents in the other project.

Now, on this WPF document I want to have a Button that upon a click can open another WPF form. Either as a new standalone WPF form, as a modal or whatever.

I cannot, on the button click event, say

WPFform2 WPFform2=new WPFform2();
WPFform2.Show();

... as many other threads on the net suggest, since the Show() method does not exist.

My solution does not allow some sort of call that changes the main Form´s ElementHost, so that is not an option for me.

All my WPF forms derives from UserControl:

public partial class WPFform1: UserControl

Solution

  • The form must be derived from Window to have the Show() method.

    Just create a new window that contains only the form you want to show and call Show on it. Or change the control's base class to Window (you will have to rewrite it both in XAML and in the code behind), nothing should really change, Window supports most of UserControl's features.