Search code examples
c#wpfwindow

how do I load a page into a frame that is in a window starting from another wpf page?


I'm creating a simple contact application in wpf and c #. Initially what I want seems simple, but I've tried in some ways to navigate between pages but it did not work. I can switch pages from my window, however, when I'm on one of my pages and try to load the other, nothing happens.

private void BtnEditarContato_Click(object sender, RoutedEventArgs e)
{
            try
            {
                Contato c = new Contato();
                c = (Contato)DgContato.SelectedItem;

                Page_Contato_Fisica p = new Page_Contato_Fisica(true, c);

                ViewContatoFisico v = new ViewContatoFisico();

                v.FrameContatoFisico.Content = p;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
}

I'm getting data from a record of a DataGrid, loading a template class and passing that class to another window to load the fields and make changes, but I'm not able to do this navigation.

Does anyone have any idea how to solve this?


Solution

  • When you say "pass that class to another window". If that window is already open then you have to go find a reference to it. You will then find it's controls are private members so you can't just set the content of a frame. It's a bad idea to use a frame at all BTW, but I'll cover that last. Add a public method to your view which is to take a page as parameter. That method is then part of your ViewContatoFisico and can make changes to it's controls.

        public void SetContent(Page p)
        {
            this.FrameContatoFisico.Content = p;
        }
    

    You then need to have a ViewContatoFisico to do anything with. You can either new one up and show it:

                Page_Contato_Fisica p = new Page_Contato_Fisica(true, c);
                ViewContatoFisico v = new ViewContatoFisico();
                v.SetContent(p);
                v.Show();
    

    In which case (if you're not careful) maybe your user ends up with loads of these open. I've seen apps where the user frequently "loses" windows because everything loads another window.

    If you already have one you showed and or want to avoid many-window problems then you could get a reference to any instance that's open instead. You could pass in typeof(ViewContatoFisico) to:

        private Window GetWindowInstance(Type winType)
        {
            Window win;
            win = Application.Current.Windows
                   .OfType<Window>()
                   .SingleOrDefault(w => w.GetType() == winType);
            if (win == null)
            {
                win = (Window)Activator.CreateInstance(winType);
            }
            return win;
        }
    

    Then, of course.

                v.SetContent(p);
                v.Show();
    

    My last piece of advice. Don't use pages and frames. Use UserControl and ContentControl instead. UserControls are almost the same as Pages but more flexible. ContentControls are much lighter weight than frames and more flexible. A frame has it's journal which holds a reference to every content you put into it.

    I also recommend you look into MVVM and viewmodel first navigation.