WindowProperties
该项目展示了一个简单的Windows应用程序,该程序使用几个属性(特性)来显示其主窗口的大小。显示内容随用户重设窗口大小而自动更新。
(1) 创建项目
创建WPF Application项目:WindowProperties。
(2) 设计界面
<Window x:Class="WindowProperties.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window Properties" Height="150" Width="300" SizeChanged="Window_SizeChanged">
<Grid>
<Label Content="Width" Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" VerticalAlignment="Top" Width="82" />
<Label Content="Height" Height="28" HorizontalAlignment="Left" Margin="184,12,0,0" VerticalAlignment="Top" Width="82" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="12,58,0,0" Name="textBoxWidth" VerticalAlignment="Top" Width="82" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="184,58,0,0" Name="textBoxHeight" VerticalAlignment="Top" Width="82" />
</Grid>
</Window>
(3) 修改MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;
namespace WindowProperties
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
sizeChanged();
}
private void sizeChanged()
{
textBoxWidth.Text = this.ActualWidth.ToString();
textBoxHeight.Text = this.ActualHeight.ToString();
}
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
sizeChanged();
}
}
}
(4) 运行测试