locked
System.InvalidOperationException: 'Window must be the root of the tree. Cannot add Window as a child of Visual.' RRS feed

  • Question

  • Hi everyone,

    I'm currently working on a project that needs to navigate between a Window and a Page. I've managed to navigate between a Window to a Page using the following code: 

                

            public void AboutButton_Click(object sender, RoutedEventArgs e)
            {
                About about = new About();
                this.Content = about.Content;

            }

    However, when I try to navigate back to my Window using this code:

            public void HomeButton_Click(object sender, RoutedEventArgs e)
            {
                MainWindow home = new MainWindow();
                this.Content = home;


            }

    It displays the error that is: System.InvalidOperationException: 'Window must be the root of the tree. Cannot add Window as a child of Visual.' I understand now that you cannot navigate back to a Window using a Page, however is there a workaround to this? Something I can implement without too much redesigning of my entire code? Please let me know if you need anymore information!

    Reece

    • Moved by CoolDadTx Tuesday, August 4, 2020 4:45 PM WPF related
    Tuesday, August 4, 2020 2:53 PM

All replies

  • Hello,

    Please use the following forum for WPF

    https://docs.microsoft.com/en-us/answers/topics/windows-wpf.html


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, August 4, 2020 3:11 PM