WPF使用Page创建显示不同控件的程序

1. 测试环境

    WIndows XP/7 + VS2010

2. 操作步骤

    1) 创建主Window的XAML文件  

<Window
	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:ee="http://schemas.microsoft.com/expression/2010/effects" mc:Ignorable="d"
	x:Class="UsePageShowControl.MainWindow"
	x:Name="Window"
	Title="UsePageShowControl"
	Width="640" Height="400" ResizeMode="CanMinimize" Background="White">

	<Grid x:Name="LayoutRoot">
		<Grid.ColumnDefinitions>
			<ColumnDefinition Width="*"/>
			<ColumnDefinition Width="3*"/>
		</Grid.ColumnDefinitions>
		
		<StackPanel Margin="3" Grid.Column="0" ButtonBase.Click="Button_Click">
			<StackPanel.Effect>
				<ee:BloomEffect BloomIntensity="2" BaseSaturation="0" BaseIntensity="1" BloomSaturation="3" Threshold="0.2"/>
			</StackPanel.Effect>
			<Button x:Name="FirstPage" Content="第一个Page" Margin="1" Height="30" FocusVisualStyle="{x:Null}"/>
			<Button x:Name="SecondPage" Content="第二个Page" Margin="1" Height="30"/>
		</StackPanel>
				
		<Frame x:Name="PageContext" Grid.Column="1" BorderBrush="Blue" BorderThickness="1" NavigationUIVisibility="Hidden"/>
	</Grid>
</Window>

    2) 创建Page1的XAML文件  

<Page
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	x:Class="UsePageShowControl.Page1"
	x:Name="Page"
	WindowTitle="Page"
	FlowDirection="LeftToRight"
	Width="450" Height="400"
	WindowWidth="640" WindowHeight="400">

	<Grid x:Name="LayoutRoot">
		<Grid.RowDefinitions>
			<RowDefinition Height="*"/>
			<RowDefinition Height="3*"/>
		</Grid.RowDefinitions>
		
		<ComboBox Grid.Row="0" Height=" 25"/>
		<ComboBox Grid.Row="1" Height=" 25"/>
	</Grid>
</Page>

    3) 创建Page2的XAML文件  

<Page
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	x:Class="UsePageShowControl.Page2"
	x:Name="Page"
	WindowTitle="Page"
	FlowDirection="LeftToRight"
	Width="450" Height="400"
	WindowWidth="640" WindowHeight="480">

	<Grid x:Name="LayoutRoot">
		<Grid.RowDefinitions>
			<RowDefinition Height="*"/>
			<RowDefinition Height="2*"/>
		</Grid.RowDefinitions>
		
		<Button Grid.Row="0" Height="30"/>
		<Button Grid.Row="1" Height="30"/>
	</Grid>
</Page>

    4) 修改主窗体的cs文件

using System;
using System.Collections.Generic;
using System.Text;
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.Shapes;

namespace UsePageShowControl
{
	/// <summary>
	/// MainWindow.xaml 的交互逻辑
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			this.InitializeComponent();

			// 在此点下面插入创建对象所需的代码。            
		}

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button button = (Button)e.Source;

            if (button.Content.ToString() == "第一个Page")
            {                
                //this.PageContext.Content = new Page1();
                this.PageContext.Source = new Uri("Page1.xaml", UriKind.Relative);
            }
            else
            {                
                this.PageContext.Source = new Uri("Page2.xaml", UriKind.Relative);
            }
        }
	}
}

编译后,就可以看到效果拉!!!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值