WPF 简单页面切换示例

原理比较简单,但是有个坑,为了使界面能够正确更新,记得使用 INotifyPropertyChanged 接口来实现属性更改通知。

html 复制代码
<Window x:Class="PageTest.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:PageTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="0.1*"/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <StackPanel Orientation="Horizontal">
            <Button Width="50" Height="25" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="5" Click="Button_Click"></Button>
            <Button Width="50" Height="25" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="5" Click="Button_Click_1"></Button>
        </StackPanel>

        <Frame Grid.Row="1" Content="{Binding CurMain}" NavigationUIVisibility="Hidden"></Frame>
    </Grid>
</Window>
csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
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 PageTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    [AddINotifyPropertyChangedInterface]
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            DataContext = this;
        }

        object curMain;

        public object CurMain { get => curMain; set => curMain = value; }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            CurMain = new Page1();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            CurMain = new Page2();
        }
    }
}
相关推荐
OfficialUser1 小时前
【C#之WPF,给Border设置背景图片,代码运行后图片无法显示】
c#·wpf
雷工笔记1 小时前
C#知识|文件与目录操作:对象的创建、保存、读取
开发语言·数据库·c#
不爱学习的啊Biao1 小时前
C# 下的限定符运算详解(全部,任意,包含)与示例
开发语言·c#
bigbig猩猩2 小时前
C#进阶-轻量级ORM框架Dapper的使用教程与原理详解
开发语言·microsoft·c#
咩咩觉主2 小时前
C# & Unity 面向对象补全计划 之 初识继承方法与多态
开发语言·学习·c#
kingwebo'sZone3 小时前
WPF使用Iconfont字符串的操作方法
wpf
NULL指向我3 小时前
C#学习笔记14:SYN6288语音模块_Winform上位机控制软件
笔记·学习·c#
技术闲聊DD4 小时前
WPF学习(2)-UniformGrid控件(均分布局)+StackPanel控件(栈式布局)
学习·wpf
Ares-Wang4 小时前
WPF 依赖属性 IsHitTestVisible
wpf
格林威6 小时前
Baumer工业相机堡盟工业相机如何通过BGAPISDK建立线程监控相机的温度(C#)
开发语言·人工智能·数码相机·计算机视觉·c#