页面框架内的WPF按钮导航以更改父页面

问题描述 投票:1回答:1

我是WPF的新手,但此刻真的很享受。我有一个关于我试图实现的页面导航的问题。

我有MainWindow.xaml,里面有一个框架。在启动时,框架加载页面1.页面1中有一个按钮。如果单击该按钮,则第2页将出现在框架内。但是,我不想要这个。当用户单击第1页中的按钮时,我希望整个页面(MainWindow.xaml)导航到第2页。所以我需要更改实际的父页面本身。

我在按钮的Click事件上使用以下代码。我只是不知道如何引用父页面。

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

如果我需要提供更多信息,请告诉我。我希望这是有道理的,任何帮助都将非常感激。谢谢。

MainWindow.xaml代码是......

<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>

Page1.xaml是......

<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是......

<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>
c# wpf navigation frame parent
1个回答
0
投票

我找到了答案。我误解了Window的工作方式。

为了解决我的问题,我做了以下事情:

在MainWindow.xaml中,我创建了一个覆盖整个窗口的框架。然后我将一个页面加载到框架中。这个页面有第二帧。在第二帧中,我加载了第二页,其中包含按钮。当我点击按钮时,我现在可以访问第一帧并导航到我想要的页面。这意味着我现在可以使用我的母版概念。

谢谢大家的帮助。你给了我理解我的问题并回答它的线索。亲切的问候。我希望这个答案有意义,并帮助其他人。

© www.soinside.com 2019 - 2024. All rights reserved.