当前位置:   article > 正文

【C#】WPF窗体在同一个位置实现不同页面切换

【C#】WPF窗体在同一个位置实现不同页面切换

关键代码看主界面代码即可

创建View文件夹,并创建用户控件

用户控件代码

UserControl1.xaml

  1. <UserControl x:Class="WpfApp15.View.UserControl1"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. xmlns:local="clr-namespace:WpfApp15.View"
  7. mc:Ignorable="d"
  8. d:DesignHeight="450" d:DesignWidth="800">
  9. <Grid Background="AliceBlue">
  10. <TextBlock Text="我是用户控件1" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="30" FontWeight="Bold"></TextBlock>
  11. </Grid>
  12. </UserControl>

UserControl2.xaml

  1. <UserControl x:Class="WpfApp15.View.UserControl1"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. xmlns:local="clr-namespace:WpfApp15.View"
  7. mc:Ignorable="d"
  8. d:DesignHeight="450" d:DesignWidth="800">
  9. <Grid Background="AliceBlue">
  10. <TextBlock Text="我是用户控件2" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="30" FontWeight="Bold"></TextBlock>
  11. </Grid>
  12. </UserControl>

UserControl3.xaml

  1. <UserControl x:Class="WpfApp15.View.UserControl1"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. xmlns:local="clr-namespace:WpfApp15.View"
  7. mc:Ignorable="d"
  8. d:DesignHeight="450" d:DesignWidth="800">
  9. <Grid Background="AliceBlue">
  10. <TextBlock Text="我是用户控件3" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="30" FontWeight="Bold"></TextBlock>
  11. </Grid>
  12. </UserControl>

主界面代码

MainWindow.xaml

  1. <Window x:Class="WpfApp15.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:local="clr-namespace:WpfApp15"
  7. mc:Ignorable="d"
  8. Title="MainWindow" Height="450" Width="800">
  9. <Grid>
  10. <Grid.ColumnDefinitions>
  11. <ColumnDefinition Width="227*"/>
  12. <ColumnDefinition Width="573*"/>
  13. </Grid.ColumnDefinitions>
  14. <StackPanel Grid.Column="0">
  15. <Button Content="切换用户控件1" Click="Button_Click"></Button>
  16. <Button Content="切换用户控件2" Click="Button_Click_1"></Button>
  17. <Button Content="切换用户控件3" Click="Button_Click_2"></Button>
  18. </StackPanel>
  19. <ContentControl Grid.Column="1" x:Name="ContentControl1"></ContentControl>
  20. </Grid>
  21. </Window>

MainWindow.xaml.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using WpfApp15.View;
  16. namespace WpfApp15
  17. {
  18. /// <summary>
  19. /// Interaction logic for MainWindow.xaml
  20. /// </summary>
  21. public partial class MainWindow : Window
  22. {
  23. public MainWindow()
  24. {
  25. InitializeComponent();
  26. }
  27. private void Button_Click(object sender, RoutedEventArgs e)
  28. {
  29. UserControl1 userControl1 = new UserControl1();
  30. ContentControl1.Content = new Frame()
  31. {
  32. Content = userControl1
  33. };
  34. }
  35. private void Button_Click_1(object sender, RoutedEventArgs e)
  36. {
  37. UserControl2 userControl2 = new UserControl2();
  38. ContentControl1.Content = new Frame()
  39. {
  40. Content = userControl2
  41. };
  42. }
  43. private void Button_Click_2(object sender, RoutedEventArgs e)
  44. {
  45. UserControl3 userControl3 = new UserControl3();
  46. ContentControl1.Content = new Frame()
  47. {
  48. Content = userControl3
  49. };
  50. }
  51. }
  52. }

结果展示

初始页面

点击切换用户控件1按钮

点击切换用户控件2按钮

点击切换用户控件3按钮

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/205491
推荐阅读
相关标签
  

闽ICP备14008679号