none
Open New WPF Window

    Question

  • I have added a new form to my WPF app. How can i open/show it from another form.

    i.e in WinForms form2.show() would work, this doesn't in WPF.

    I have seen how to do by declaring a new form programmatically, but woul like to open a form i have designed etc.

    any thoughts welcome, 

    Cheers 
    Monday, January 19, 2009 4:10 PM

Answers

  • Lets go through this a little bit in detail. 

    1. You have a WPF project and It already has a window. You are able to run this application.

    2. Now you right click on the project and Add new Window. You name it MyNewWindow.xaml

    3. You would now notice MyNewWindow.xaml and MyNewWindow.xaml.cs added to your project. (the class name for the window would be MyNewWindow which is in the .xaml.cs file and it derives from Window; also a partial class)

    4. Open the XAML file for the MyNewWindow (MyNewWindow.xaml) and add your controls. Treat it like any other window and write code.

    5. Now in your main window (the first one) you add a Button which when clicked should show the newly created window.

    For that inside the Click handler, .... 

    var newWindow = new MyNewWindow();
    newWindow.Show();


    This would create a new instance of the MyNewWindow and shows it to the user. It would appear just as you designed in your XAML.

    If this is an issue, let me know so that I will upload a sample application and link you with it.

    Research and Development Imageright Inc. http://krishnabhargav.blogspot.com
    • Marked as answer by Paul Cranness Tuesday, January 20, 2009 8:44 AM
    Monday, January 19, 2009 4:38 PM

All replies

  • Did you add a new WPF window or a new WPF Page. Both has different ways to show up. 

    If its window, then use

    var newW  = new NewWindow();
    newW.Show(); // works


    If it is page, then you need to create a new NavigationWindow and show the page using Navigate() method of that window, passing the Page you created.

    Research and Development Imageright Inc. http://krishnabhargav.blogspot.com
    Monday, January 19, 2009 4:15 PM
  • I added a new Window to the project (right click add window...).

    The way shown above seems to declare a new window which is completely blank.

    I want to add controls/code to a window, and then open it when the users clicks on a Next button. E.g. Similar to what happen in a setup application
    Monday, January 19, 2009 4:29 PM
  • Lets go through this a little bit in detail. 

    1. You have a WPF project and It already has a window. You are able to run this application.

    2. Now you right click on the project and Add new Window. You name it MyNewWindow.xaml

    3. You would now notice MyNewWindow.xaml and MyNewWindow.xaml.cs added to your project. (the class name for the window would be MyNewWindow which is in the .xaml.cs file and it derives from Window; also a partial class)

    4. Open the XAML file for the MyNewWindow (MyNewWindow.xaml) and add your controls. Treat it like any other window and write code.

    5. Now in your main window (the first one) you add a Button which when clicked should show the newly created window.

    For that inside the Click handler, .... 

    var newWindow = new MyNewWindow();
    newWindow.Show();


    This would create a new instance of the MyNewWindow and shows it to the user. It would appear just as you designed in your XAML.

    If this is an issue, let me know so that I will upload a sample application and link you with it.

    Research and Development Imageright Inc. http://krishnabhargav.blogspot.com
    • Marked as answer by Paul Cranness Tuesday, January 20, 2009 8:44 AM
    Monday, January 19, 2009 4:38 PM
  • Remember that "MyNewWindow" that KrishnaBhargava is saying is the name of the class. Now if your namespace is different than the one you are using within the window that is calling it you will also need to call that namespace.

    If by chance you created a new project class then you need to "add the reference" by right clicking and click on "add reference" located under your project within the "Solution Explorer" tab

    MyNewWindow newWindow = new MyNewWindow();
    newWindow.Show(); //  Shows a window
    OR
    newWindow.ShowDialog(); // shows a window and makes the parent function wait till the window is closed



    Hope that helps
    Monday, January 19, 2009 8:51 PM
  • Thanks Guys,

    That has proved very useful, and i can now achieve what i need to.

    Paul
    Tuesday, January 20, 2009 8:43 AM
  • Guys,

    I have a requirement similar to this.

    We have a third party chart control in our WPF window.

    On click of this chart control I have to show that chart control for the entire screen of the monitor just like youtube video when we click on FULL SCREEN option.

    again on click of ESC key I should be able to see the parent window.

    how I can open the new window with passing the information from the parent window with the chart control data.

    Do anyone has idea for this?

    Please let me know.

    Thanks in Advance,
    Sudhakar
    Thursday, May 28, 2009 6:16 AM
  • Guys,

    I have a requirement similar to this.

    We have a third party chart control in our WPF window.

    On click of this chart control I have to show that chart control for the entire screen of the monitor just like youtube video when we click on FULL SCREEN option.

    again on click of ESC key I should be able to see the parent window.

    how I can open the new window with passing the information from the parent window with the chart control data.

    Do anyone has idea for this?

    Please let me know.

    Thanks in Advance,
    Sudhakar

    Hi Sudhakar,


    I am posting the code first .....

    Here assume Window2 to be the parent window from where you will pass the chart control to Window3.
    Here instead of passing a chart control i am just passing a button.

    In Window2.xaml your parent window


    <Window x:Class="forims.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window2" WindowState="Maximized">
        <Grid>
            <StackPanel>
                <Button x:Name="btn1" Margin="50" Height="30" Width="100" Content="NewWindow" Click="btn1_Click"></Button>
                
            </StackPanel>    
        </Grid>
    </Window>

    In Window2.xaml.cs



    namespace forims
    {
        /// <summary>
        /// Interaction logic for Window2.xaml
        /// </summary>
        public partial class Window2 : Window
        {
            public Window2()
            {
                InitializeComponent();
            }
    
            private void btn1_Click(object sender, RoutedEventArgs e)
            {
                Button btnControlPass = new Button();
                btnControlPass.Content = "HellofromParentWindow";
                Window3 win3 = new Window3(btnControlPass);
                win3.Show();
            }        
        }
    }
    In Window3.xaml your child window



    <Window x:Class="forims.Window3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window3" Height="300" Width="300">
        <Grid x:Name="myGrid">
            
        </Grid>
    </Window>
    In Window3.xaml.cs


    namespace forims
    {
        /// <summary>
        /// Interaction logic for Window3.xaml
        /// </summary>
        public partial class Window3 : Window
        {
            public Window3()
            {
                InitializeComponent();
                
            }
    
            void Window3_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.Key.Equals(Key.Escape))
                {
                    this.Close();
                }
            }
    
            public Window3(Button btnObj)
            {
                InitializeComponent();
    
                Button b = (Button)btnObj;
                this.myGrid.Children.Add(b);
                b.Click += new RoutedEventHandler(b_Click);
                b.KeyDown += new KeyEventHandler(Window3_KeyDown);
            }
    
            void b_Click(object sender, RoutedEventArgs e)
            {
                this.WindowState = WindowState.Maximized;
            }
        }
    }
    Cheers,
    Tarun
    Developer
    • Proposed as answer by Tarun Jindal Thursday, May 28, 2009 7:33 AM
    Thursday, May 28, 2009 7:32 AM
  • Hi,

    Steps to execute the above code....

    Run the application and you will see a full screen window with a button as NewWindow
    Click on that button and a new Window will open in a smaller size with the button that you passed from the ParentWindow in your case the chart control

    Click on that button and the child window will be in maximized state

    Press the ESC key now and the child window will close and you will be able to see the Parent WIndow.

     

    Hope it Helps.

    Please mark the posts as answered it your problem has been solved.

     

     


    Developer
    • Proposed as answer by Tarun Jindal Thursday, May 28, 2009 7:38 AM
    Thursday, May 28, 2009 7:38 AM
  • Hey Tarun i tried the above code but it throws an error saying " 'notification.Window3' does not contain a constructor that takes '1' arguments"    G:\Hospital manage\R&D\notification\notification\Window2.xaml.cs    .....

     

     

     

    i have tried the same code that you have specified over here:

    private void btn1_Click(object sender, RoutedEventArgs e)
            {
                Button btnControlPass = new Button();
                btnControlPass.Content = "HellofromParentWindow";
                Window3 win3 = new Window3(btnControlPass);    .................[This is where it gives me the error]
                win3.Show();

            }

    Friday, May 13, 2011 4:52 AM
  • I also wanted to achieve same functionality but with slight change.

    my application uses PRISM & MEF all over the project.

    now my this new window contains a Grid that gets its ItemsSource(i.e. collection) from a ViewModel which supports MEF.

    I tried to set datacontext of 'MyNewWindow' same as it was on MainWindow follows :

    [Import]
            public TestHarnessViewModel XmlViewModel
            {
                set { DataContext = value; }
            }

    but its not working.

    Do I have to register this MyNewWindow in bootstrapper or what?

    I was new to this MEF library.

    plz help me out.


    Dheeraj

    Thursday, September 27, 2012 5:25 PM