首页 > 其他分享 >WPF手动实现切换页面

WPF手动实现切换页面

时间:2024-08-28 23:14:59浏览次数:5  
标签:手动 Two new PageDict PageType currentPage WPF public 页面

1、首先主页面

  <Grid>
      <Grid.ColumnDefinitions>
          <ColumnDefinition Width="220" />
          <ColumnDefinition />
      </Grid.ColumnDefinitions>

      <Grid Grid.Column="1">
          <Grid.RowDefinitions>
              <RowDefinition />
              <RowDefinition Height="auto"/>
          </Grid.RowDefinitions>
          <ContentControl Content="{Binding CurrentView}" />
          <Button
              Width="200"
              Height="30"
              Grid.Row="1"
              HorizontalAlignment="Right"
              VerticalAlignment="Bottom"
              Command="{Binding NextPageCommand}"
              Content="{Binding Content}" />
      </Grid>
  </Grid>

2、接着ViewModel代码

 public class MainWindowViewModel : BindableBase
 {
     private PageType _currentPage = PageType.One;

     public string Content
     {
         get => _currentPage == PageType.One ? "下一页" : "返回";
         set => SetProperty(ref _currentPage, value == "下一页" ? PageType.One : PageType.Two);
     }

     private UserControl _currentView;

     public UserControl CurrentView
     {
         get => _currentView;
         set => SetProperty(ref _currentView, value);
     }

     public ICommand NextPageCommand { get; set; }

     public MainWindowViewModel()
     {
         NextPageCommand = new DelegateCommand(Execute);
         CurrentView = MainContent.PageDict[PageType.One.ToString()];
     }

     private void Execute()
     {
         _currentPage = _currentPage == PageType.One ? PageType.Two : PageType.One;
         Content = Content; 
         CurrentView = MainContent.PageDict[_currentPage.ToString()];
     }
 }

 public class MainContent
 {
     public static Dictionary<string, UserControl> PageDict = new Dictionary<string, UserControl>();
    
     static MainContent()
     {
         Init();
     }
     public static void Init()
     {
         PageDict.Add("One", new OnePageView());
         PageDict.Add("Two", new TwoPageView());
     }
 }
 public enum PageType
 {
     One,
     Two
 }

标签:手动,Two,new,PageDict,PageType,currentPage,WPF,public,页面
From: https://www.cnblogs.com/guchen33/p/18385691

相关文章

  • WPF 现代化开发教程:使用 Microsoft.Extensions.Hosting 和 CommunityToolkit.Mvvm
    介绍随着WPF应用程序的复杂性增加,使用现代化的开发工具和模式变得尤为重要。本教程将指导你如何使用Microsoft.Extensions.Hosting和CommunityToolkit.Mvvm来开发一个现代化的WPF应用程序。这些工具为开发者提供了依赖注入、应用程序生命周期管理、MVVM模式支持等功能。先决......
  • WPF draw thumbs on livecharts and drag drop thumb
    //xaml<Windowx:Class="WpfApp299.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • [Azure Application Insights]Azure应用程序见解概述页面中workspace的link不见了?
    问题描述在AzureApplicationInsights的概述页面中,可以直接点击WorkspaceLink进入到Workspace资源页面。但是,在下面的示例图中,WorkspaceLink不见了?这是什么原因呢? 问题解答这是因为Workspace的资源组发生了改变。ApplicationInsights无法根据WorksapceResour......
  • WPF thumb Drag DragDelta,DragStarted,DragCompleted
    //xaml<Windowx:Class="WpfApp302.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • 自适应高度(子页面)
    <div><iframeid="sonList3"src="${webRoot}/pdmQcApplyController.do?goZh3"frameborder="0"width="1300px"onload="resizeIframe(this)"></iframe></d......
  • 3D高斯渲染 (1)手动窗口可视化
       ##Copyright(C)2023,Inria#GRAPHDECOresearchgroup,https://team.inria.fr/graphdeco#Allrightsreserved.##Thissoftwareisfreefornon-commercial,researchandevaluationuse#underthetermsoftheLICENSE.mdfile.##Forinquirie......
  • UIOTOS来开发界面,相比Qt、QML、VC、WPF、C#,有什么优劣势
    使用UIOTOS来开发界面,与Qt、QML、VC(VisualC++)、WPF、C#相比,具有其独特的优劣势。以下是对这些工具在开发界面方面的优劣势进行的详细分析:UIOTOS的优势低代码/无代码开发:UIOTOS作为一款前端零代码工具,显著降低了开发门槛。用户无需编写代码,只需通过拖拽、配置、连线等操作即......
  • 是否有一款低代码或零代码工具,能代替或减少VC、Qt、C#、WinForm、WPF等前端界面开发工
    确实存在低代码或零代码工具,能够代替或减少VC(VisualC++)、Qt、C#、WinForm、WPF等前端界面开发的工作量。这些工具通过提供图形用户界面(GUI)和可视化开发工具,使得开发者无需或仅需少量编写代码即可快速搭建出功能丰富的应用程序界面。低代码/零代码工具的优势降低开发门槛:这些工......
  • WPF LiveCharts.Wpf
    //xaml<Windowx:Class="WpfApp299.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • FastReport .NET WPF 24.2.17 Crack
    FastReport.NETWPFAsetofhigh-performancecomponentsforcreatingreportsanddocumentsinWPFprojectsBuyTryforfreeWhatreportscanFastReport.NETWPFdo?Practicallyany:invoices,financialreports,productcatalogswithcolorprofilesuppor......