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 }