WPF頁面切換及彈窗


WPF頁面切換及彈窗

結構如圖:

效果如圖:

代碼如下:

xaml

 1 <Window x:Class="PageChange.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         Title="MainWindow" Height="350" Width="400">
 5     <Grid>
 6         <Grid.ColumnDefinitions>
 7             <ColumnDefinition Width="100"></ColumnDefinition>
 8             <ColumnDefinition Width="300"></ColumnDefinition>
 9         </Grid.ColumnDefinitions>
10         <StackPanel Orientation="Vertical" Grid.Column="0">
11             <Button Content="第一頁" Name="tb1" Click="tb1_Click"></Button>
12             <Button Content="第二頁" Name="tb2" Click="tb2_Click"></Button>
13             <Button Content="第三頁" Name="tb3" Click="tb3_Click"></Button>
14             <Button Content="彈窗" Name="tb4" Click="tb4_Click"></Button>
15         </StackPanel>
16         <Grid Grid.Column="1" Name="MyGrid" Height="300" Width="300"></Grid>
17        
18     </Grid>
19 </Window>

后置代碼

 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 
16 namespace PageChange
17 {
18     /// <summary>
19     /// MainWindow.xaml 的交互邏輯
20     /// </summary>
21     public partial class MainWindow : Window
22     {
23         public MainWindow()
24         {
25             InitializeComponent();
26         }
27 
28         private void tb1_Click(object sender, RoutedEventArgs e)
29         {
30             MyGrid.Children.Add(new Page1());
31         }
32 
33         private void tb2_Click(object sender, RoutedEventArgs e)
34         {
35             MyGrid.Children.Add(new Page2());
36         }
37 
38         private void tb3_Click(object sender, RoutedEventArgs e)
39         {
40             MyGrid.Children.Add(new Page3());
41         }
42 
43         private void tb4_Click(object sender, RoutedEventArgs e)
44         {
45             Window w = new Window();
46             NewPage n = new NewPage();
47             w.Content = n;
48             w.ShowDialog();
49         }
50     }
51 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM