Opening a new WPF form from another WPF form

18,512

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.

Share:
18,512
Farsen
Author by

Farsen

Updated on June 07, 2022

Comments

  • Farsen
    Farsen almost 2 years

    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();<br>
    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