Search code examples
wpfframes

How do I open page in different frame - WPF


I have 2 frames in a WPF C# window. Upon loading, the application loads a page into the left frame and nothing into the right frame. After navigating through 2 pages in the left frame I am trying to get a different page to load in the right frame. The only way I have been able to do this is to close the window and reload it.

How can I load a new page in the right frame of a WPF window upon a button click on a different page in the left frame on the same WPF window without reloading the hole window?

Thank You

edit:

To clarify a bit, I have 1 Window with 2 frames. I want to open a page in the right frame by clicking a button on a page in the left frame. This is what I have:

variables class holding a "test" int:

    public static class variables
    {
        public static int test;
    }

Main Window:

    public MainWindow()
    {
        InitializeComponent();
        int mytest = variables.test;
        left1 left1 = new left1();
        left.NavigationService.Navigate(left1);
        Right1 right1 = new Right1();
        if (mytest == 1)
        {
            right.NavigationService.Navigate(right1);
            MessageBox.Show("1");
        }
    }

And the left1 page:

 public left1()
    {
        InitializeComponent();

    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        int test = 1;
        variables.test = test;
        MainWindow mw = new MainWindow();
        mw.Show();
    }

This works BUT it opens another window on calling mw.Show(); Is there a way for this to work without opening another window (like refreshing the window)? I tried mw.InitializeComponent(); and that would show the MessageBox but would not re render the MainWindow. I have tried to create a Method within MainWindow and call that on the left1 page button click but it does the same thing ... shows the MessageBox but does not refresh the MainWindow.


Solution

  • OK if you havent figured it out Im new! ... I have many years ago learned Basic, Pascal, and C. And then did some web developing 10 years ago, but its been a long time. I figured it out!! I have only been working with C# WPF for about 2 weeks now.

    I bound the Visibility property of the right frame to a method under the MainWindow class which is called on a button click and used INotifyPropertyChanged Event. This is what I have now:

    MainWindow.xmal.cs:

        public class Refresh : INotifyPropertyChanged
    {
        private string vis1;
        public string Vis1
        {
            get { return vis1; }
            set
            {
                if (vis1 != value)
                {
                    vis1 = value;
                    OnPropertyChanged("Vis1");
                }
            }
        }
        protected void OnPropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
    
    public partial class MainWindow : Window
    {
        private static Refresh Visi;
        public MainWindow()
        {
            InitializeComponent();
            Left1 l1 = new Left1();
            Right1 r1 = new Right1();
            left.NavigationService.Navigate(l1);
            right.NavigationService.Navigate(r1);
            Visi = new Refresh { Vis1 = "Hidden" };
            DataContext = Visi;
        } 
        public static void showRight()
        {
            Visi.Vis1 = "Visible";
        }
    }
    

    The XAML:

    <Frame x:Name="right" Content="Frame" HorizontalAlignment="Left" Height="224" Margin="262,10,0,0" VerticalAlignment="Top" Width="225" Visibility="{Binding Vis1}" />
    

    And just a simple button click event on the Left page!:

        public partial class Left1 : Page
    {
        public Left1()
        {
            InitializeComponent();
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MainWindow.showRight();
        }
    }
    

    Hope this helps someone else out there! Only took me a day and a half to figure it out! LOL My first successful binding.

    So now in this code the right frame has a page loaded but is hidden from view and the left frames page is shown. When the button is clicked on the left page the right frame becomes unhidden!