WPF [SOLVED]: WPF Button within a frame in a page navigates to change the parent page

WPF [SOLVED]: WPF Button within a frame in a page navigates to change the parent page

Home Forums Frameworks WPF WPF [SOLVED]: WPF Button within a frame in a page navigates to change the parent page

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #246863

    Cloudy Point
    Keymaster

    QuestionQuestion

    I am very new to WPF but really enjoying it at the moment. I have a question about page navigation that I am trying to implement.

    I have the MainWindow.xaml which has a frame inside it.
    On startup, the frame loads page 1.
    Page 1 has a button in it.
    If you click on the button, page two appears inside the frame.
    However, I don’t want this. When the user clicks on the button in page 1, I want the whole page(MainWindow.xaml) to navigate to page 2. So I need the actual parent page itself to change.

    I am using the following code on the button’s Click event. I just don’t know how to refer to the parent page.

    private void Button_Click(object sender, RoutedEventArgs e)
            {
                NavigationService.Navigate(new PageTwoReplacesMainPage());
            }
    

    Please let me know if I need to provide more information.
    I hope this makes sense and any help would be very appreciated. Thank you.

    The MainWindow.xaml code is…

    <Window x:Class="TestWpfParentNavigation.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:TestWpfParentNavigation"
            mc:Ignorable="d"
            Title="MainWindow" MinHeight="500" MinWidth="700" Background="Beige">
        <Grid>
            <StackPanel>
                <Frame x:Name="myMainFrame" NavigationUIVisibility="Hidden" Height="400" Width="600" Margin="0,50,0,0" />
            </StackPanel>
        </Grid>
    </Window>
    

    The Page1.xaml is…

    <Page x:Class="TestWpfParentNavigation.PageOneWithButton"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
          xmlns:local="clr-namespace:TestWpfParentNavigation"
          mc:Ignorable="d" 
          d:DesignHeight="200" d:DesignWidth="300"
          Title="PageOneWithButton" Background="Bisque">
    
        <Grid>
            <Button Content="Go to the second page" Height="30" Width="150" Click="Button_Click" Margin="10,10,140,160" />
        </Grid>
    </Page>
    

    Page2.xaml is…

    <Page x:Class="TestWpfParentNavigation.PageTwoReplacesMainPage"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
          xmlns:local="clr-namespace:TestWpfParentNavigation"
          mc:Ignorable="d" 
          d:DesignHeight="300" d:DesignWidth="300"
          Title="PageTwoReplacesMainPage" Background="Aqua">
    
        <Grid>
    
        </Grid>
    </Page>
    

    #246864

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    I have found the answer. I misunderstood the way a Window is supposed to work.

    To solve my problem, I did the following:

    In the MainWindow.xaml, I created a frame which covered the whole window.
    Then I loaded a page into the frame. This page had a second frame in it.
    In the The second frame, I load the second page which has the button.
    When I click on the button, I can now access the very first frame and navigate to the page that I wanted. This means I can utlise my masterpage concept now.

    Thank you all for trying to help. You gave me the clues to understand my problem and answer it. Kind regards. I hope this answer makes sense and helps anyone else.

    Source: https://stackoverflow.com/questions/47945400/wpf-button-within-a-frame-in-a-page-navigates-to-change-the-parent-page
    Author: Irfan
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.