第一章 窗体的基本属性
<Window x:Class="zhaoxi.MainWindow"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:local="clr-namespace:zhaoxi"mc:Ignorable="d"SizeToContent="Manual" WindowStartupLocation="CenterScreen"Icon="image/logo.ico"Title="MainWindow" Height="450" Width="800" MaxWidth="1200" MinWidth="500"><!-- SizeToContent="WidthAndHeight" 默认窗体大小随内容宽高 --><!-- Icon="image/logo.ico" 窗体的图标设置(右键图片属性:生产操作:资源)--><!-- 应用程序的图标:右键项目属性 >> 应用程序 >> win32资源 >> 图标(需要icon文件) --><!-- WindowStartupLocation="CenterScreen" 窗体启动位置--><!-- MaxWidth="1200" MinWidth="500" 最大宽高和最小宽--><Grid><Button Content="点击打开新窗口" Width="100" Height="20" x:Name="btn" Click="btn_Click"></Button><!-- x:Name="btn" 定义了一个名为 btn 的标识符,它对应于这个 <Button> 元素。在代码后面(例如C#代码中),你可以使用 btn 这个名字来直接访问这个按钮,进行诸如改变它的 Content、Width、Height 属性或添加点击事件处理器等操作。--><!-- Click="btn_Click" 点击事件--></Grid> </Window>
子窗体打开在父窗体正中间
using chapter1; 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 zhaoxi {/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();}/// <summary>/// 点击打开子窗体事件/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btn_Click(object sender, RoutedEventArgs e){Window1 window1 = new Window1();window1.Owner = this; //窗体的所有者是当前主窗体 window1.Show();}} }
子窗体 Window1
<Window x:Class="chapter1.Window1"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:local="clr-namespace:chapter1"mc:Ignorable="d"WindowStartupLocation="CenterScreen"Topmost="True"Title="Window1" Height="250" Width="400"><Grid><Button Content="只是一个新窗口" Width="200" Height="50" FontSize="20"/></Grid> </Window>
效果