Home > Software engineering >  How can I open a page in the same frame when click a button in that frame in wpf?
How can I open a page in the same frame when click a button in that frame in wpf?

Time:07-28

I have a frame and there is page with 2 buttons in that frame. I want to open another page in that same frame when click a button on previous page. I know how to open the page when initializing the frame.

public partial class PnlTabs_SM : Window
{
    public PnlTabs_SM()
    {
        InitializeComponent();
        frame_SM.NavigationService.Navigate(new Uri("SubPnlWelcomeTabs.xaml", UriKind.Relative));
    } 
}

Here you can find the Page Code:

 public partial class SubPnlWelcomeTabs : Page
{
    public SubPnlWelcomeTabs()
    {
        InitializeComponent();
    }

    private void btnPurchaseRequistions_Click(object sender, RoutedEventArgs e)
    { 
        
    }
}

the page which I want to open:

public partial class SubPnlPRTabs : Page
{
    public SubPnlPRTabs()
    {
        InitializeComponent();
    }
}

XAML Frame Code:

<Frame x:Name="frame_SM"
       x:FieldModifier="public"
       NavigationUIVisibility="Hidden"
       Width="950" 
       Height="460"/>

I want to display the SubPnlPRTabs page in the same frame called "frame_SM" when click btnPurchaseRequistions

Kind consideration is highly appriciated. Thank you!

CodePudding user response:

add public method for changing page, e.g:

public partial class PnlTabs_SM : Window
{
    public PnlTabs_SM()
    {
        InitializeComponent();
        Navigate("SubPnlWelcomeTabs.xaml");
    } 

    public void Navigate(string page)
    {
        frame_SM.NavigationService.Navigate(new Uri(page, UriKind.Relative));
    }
}

and call it from open pages:

public partial class SubPnlWelcomeTabs : Page
{
    public SubPnlWelcomeTabs()
    {
        InitializeComponent();
    }

    private void btnPurchaseRequistions_Click(object sender, RoutedEventArgs e)
    { 
        var w = Application.Current.Windows.OfType<PnlTabs_SM>().First();
        w.Navigate("SubPnlPRTabs.xaml");
    }
}

You might also want to look at navigation based on DataTemplates: WPF MVVM navigate views

  • Related