一、窗体类
在Visual Studio和Expression Blend中,自定义的窗体 均继承System.Windows.Window类(类型化窗体)。定义的窗体由两部分组成:
1、XAML文件
1: <Window
2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation& quot;
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: x:Class="WpfWindow.BasicWindow"
5: x:Name="Window"
6: Title="BasicWindow"
7: Width="300" Height="200">
8: <Canvas>
9: <Button x:Name="btnMessage" Width="79" Height="24" Content="OK"
10: Canvas.Left="172" Canvas.Top="93" Click="btnMessage_Click"/>
11: <TextBox x:Name="txtValue" Width="215" Height="25"
12: Canvas.Left="36" Canvas.Top="48" Text="" TextWrapping="Wrap"/>
13: </Canvas>
14: </Window>
2、后台代码文件
1: using System;
2: using System.Windows;
3:
4: namespace WpfWindow
5: {
6: public partial class BasicWindow : Window
7: {
8: public BasicWindow()
9: {
10: this.InitializeComponent();
11: }
12:
13: private void btnMessage_Click(object sender, System.Windows.RoutedEventArgs e)
14: {
15: txtValue.Text = "Hello World";
16: }
17: }
18: }
也可以将后台代码放在XAML文 件中,上面的例子可以改写为:
1: <Window
2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation& quot;
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: x:Class="WpfWindow.BasicWindow"
5: x:Name="Window"
6: Title="BasicWindow"
7: Width="300" Height="200">
8: <Canvas>
9: <Button x:Name="btnMessage" Width="79" Height="24" Content="OK"
10: Canvas.Left="172" Canvas.Top="93" Click="btnMessage_Click"/>
11: <x:Code><![CDATA[
12: void btnMessage_Click(object sender, System.Windows.RoutedEventArgs e)
13: {
14: txtValue.Text = "Hello World";
15: }
16: ]]>
17: </x:Code>
18: <TextBox x:Name="txtValue" Width="215" Height="25"
19: Canvas.Left="36" Canvas.Top="48" Text="" TextWrapping="Wrap"/>
20: </Canvas>
21: </Window>