现在的位置: 首页 > 综合 > 正文

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

2013年06月13日 ⁄ 综合 ⁄ 共 2918字 ⁄ 字号 评论关闭

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

<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

<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);
            }
        }
	}
}

 

 

 

 

 

 

 

 

 

 

 

抱歉!评论已关闭.