WPF frame navigate to pages and title show page title

//mainwindow.xaml
<Window x:Class="WpfApp233.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:WpfApp233"
        WindowState="Maximized"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Frame x:Name="mFrame" Navigated="mFrame_Navigated" />
    </Grid>
</Window>


//mainwindow.xaml.cs
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfApp233.Pages;

namespace WpfApp233
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            MainPage mPage=new MainPage();
            mFrame.Navigate(mPage);
        }

        private void mFrame_Navigated(object sender, NavigationEventArgs e)
        {
            var pg=e.Content as Page;
            if(pg!=null)
            {
                this.Title = pg.Title;
            }
        }
    }
}


//MainPage.xaml
<Page x:Class="WpfApp233.Pages.MainPage"
      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:WpfApp233.Pages"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="MainPage">
    <Page.Resources>
        <Style TargetType="{x:Type Button}">
            <Setter Property="FontSize" Value="50"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}">
                            <ContentPresenter HorizontalAlignment="Center"
                                              VerticalAlignment="Center"
                                              RecognizesAccessKey="True"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Page.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <Button Grid.Row="0"
                Grid.Column="0"
                Content="1"
                Click="Button_Click">
            <Button.Background>
               <ImageBrush ImageSource="pack://application:,,,/Images/1.jpg"
                            Stretch="Uniform"/>
            </Button.Background>
        </Button>

        <Button Grid.Row="0"
                Grid.Column="1"
                Content="2"
                Click="Button_Click">
            <Button.Background>
                <ImageBrush ImageSource="pack://application:,,,/Images/2.jpg"
                            Stretch="Uniform"/>
            </Button.Background>
        </Button>

        <Button Grid.Row="0"
                Grid.Column="2"
                Content="3"
                Click="Button_Click">
            <Button.Background>
                <ImageBrush ImageSource="pack://application:,,,/Images/3.jpg"
                            Stretch="Uniform"/>
            </Button.Background>
        </Button>

        <Button Grid.Row="0"
                Grid.Column="3"
                Content="4"
                Click="Button_Click">
            <Button.Background>
                <ImageBrush ImageSource="pack://application:,,,/Images/4.jpg"
                            Stretch="Uniform"/>
            </Button.Background>
        </Button>
        
        <Button Grid.Row="0"
                Grid.Column="4"
                Content="5"
                Click="Button_Click">
            <Button.Background>
                <ImageBrush ImageSource="pack://application:,,,/Images/5.jpg"
                            Stretch="Uniform"/>
            </Button.Background>
        </Button>
        
        <Button Grid.Row="1"
                Grid.Column="0"
                Content="6"
                Click="Button_Click">
            <Button.Background>
                <ImageBrush ImageSource="pack://application:,,,/Images/6.jpg"
                            Stretch="Uniform"/>
            </Button.Background>
        </Button>
        
        <Button Grid.Row="1"
                Grid.Column="1"
                Content="7"
                Click="Button_Click">
            <Button.Background>
                <ImageBrush ImageSource="pack://application:,,,/Images/7.jpg"
                            Stretch="Uniform"/>
            </Button.Background>
        </Button>

        <Button Grid.Row="1"
                Grid.Column="2"
                Content="8"
                Click="Button_Click">
            <Button.Background>
                <ImageBrush ImageSource="pack://application:,,,/Images/8.jpg"
                            Stretch="Uniform"/>
            </Button.Background>
        </Button>

        <Button Grid.Row="1"
                Grid.Column="3"
                Content="9"
                Click="Button_Click">
            <Button.Background>
                <ImageBrush ImageSource="pack://application:,,,/Images/9.jpg"
                            Stretch="Uniform"/>
            </Button.Background>
        </Button>

        <Button Grid.Row="1"
                Grid.Column="4"
                Content="10"
                Click="Button_Click">
            <Button.Background>
                <ImageBrush ImageSource="pack://application:,,,/Images/10.jpg"
                            Stretch="Uniform"/>
            </Button.Background>
        </Button>
    </Grid>
</Page>


//mainpage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp233.Pages
{
    /// <summary>
    /// Interaction logic for MainPage.xaml
    /// </summary>
    public partial class MainPage : Page
    {
        List<Page> pagesList = new List<Page>();
        int idx = 0; 
        private Frame mf; 
        public MainPage()
        {
            InitializeComponent();
            InitPagesLis();
            mf = Application.Current?.MainWindow.FindName("mFrame") as Frame;
        }

        private void InitPagesLis()
        {
            pagesList.Add(new Page1());
            pagesList.Add(new Page2());
            pagesList.Add(new Page3());
            pagesList.Add(new Page4());
            pagesList.Add(new Page5());
            pagesList.Add(new Page6());
            pagesList.Add(new Page7());
            pagesList.Add(new Page8());
            pagesList.Add(new Page9());
            pagesList.Add(new Page10());
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var btn = sender as Button;
                if(btn!=null)
                {
                    string idxStr = btn.Content?.ToString();
                    Int32.TryParse(idxStr, out idx);
                    if (mf != null)
                    {
                        var tempPage = pagesList[idx-1];
                        mf.Navigate(tempPage);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}


//Page1
<Page x:Class="WpfApp233.Pages.Page1"
      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:WpfApp233.Pages"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="Page1">

    <Grid>
        <Grid.Background>
            <ImageBrush ImageSource="pack://application:,,,/Images/1.jpg"
                        Stretch="Uniform"/>
        </Grid.Background>
    </Grid>
</Page>


//Page2
<Page x:Class="WpfApp233.Pages.Page2"
      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:WpfApp233.Pages"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="Page2">

    <Grid>
        <Grid.Background>
            <ImageBrush ImageSource="pack://application:,,,/Images/2.jpg"
                Stretch="Uniform"/>
        </Grid.Background>
    </Grid>
</Page>


//Page3
<Page x:Class="WpfApp233.Pages.Page3"
      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:WpfApp233.Pages"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="Page3">

    <Grid>
        <Grid.Background>
            <ImageBrush ImageSource="pack://application:,,,/Images/3.jpg"
                Stretch="Uniform"/>
        </Grid.Background>
    </Grid>
</Page>


//Page4
<Page x:Class="WpfApp233.Pages.Page4"
      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:WpfApp233.Pages"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="Page4">

    <Grid>
        <Grid.Background>
            <ImageBrush ImageSource="pack://application:,,,/Images/4.jpg"
                Stretch="Uniform"/>
        </Grid.Background>
    </Grid>
</Page>


//Page5
<Page x:Class="WpfApp233.Pages.Page5"
      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:WpfApp233.Pages"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="Page5">

    <Grid>
        <Grid.Background>
            <ImageBrush ImageSource="pack://application:,,,/Images/5.jpg"
                Stretch="Uniform"/>
        </Grid.Background>
    </Grid>
</Page>


//Page6
<Page x:Class="WpfApp233.Pages.Page6"
      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:WpfApp233.Pages"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="Page6">

    <Grid>
        <Grid.Background>
            <ImageBrush ImageSource="pack://application:,,,/Images/6.jpg"
                Stretch="Uniform"/>
        </Grid.Background>
    </Grid>
</Page>


//Page7
<Page x:Class="WpfApp233.Pages.Page7"
      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:WpfApp233.Pages"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="Page7">

    <Grid>
        <Grid.Background>
            <ImageBrush ImageSource="pack://application:,,,/Images/7.jpg"
                Stretch="Uniform"/>
        </Grid.Background>
    </Grid>
</Page>


//Page8
<Page x:Class="WpfApp233.Pages.Page8"
      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:WpfApp233.Pages"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="Page8">

    <Grid>
        <Grid.Background>
            <ImageBrush ImageSource="pack://application:,,,/Images/8.jpg"
                Stretch="Uniform"/>
        </Grid.Background>
    </Grid>
</Page>



//Page9
<Page x:Class="WpfApp233.Pages.Page9"
      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:WpfApp233.Pages"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="Page9">

    <Grid>
        <Grid.Background>
            <ImageBrush ImageSource="pack://application:,,,/Images/9.jpg"
                Stretch="Uniform"/>
        </Grid.Background>
    </Grid>
</Page>


//Page10
<Page x:Class="WpfApp233.Pages.Page10"
      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:WpfApp233.Pages"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="Page10">

    <Grid>
        <Grid.Background>
            <ImageBrush ImageSource="pack://application:,,,/Images/10.jpg"
                Stretch="Uniform"/>
        </Grid.Background>
    </Grid>
</Page>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2025-05-18 00:30  FredGrit  阅读(9)  评论(0)    收藏  举报