WPF入门到进阶-窗体属性
阅读声明本文立志作为WPF常用百科全书因此引用了部分网络文章及AI制作可以供大家进行查阅。WPF程序结构主要由xaml和cs文件组成其中xaml称为前端、cs文件称为后端。窗体属性WPF提供了非常灵活的开发模式既可以在XAML前端中声明式地设置属性也可以在C#后端中命令式地修改属性两者可以混合使用。XAML方式适合静态属性、界面布局、样式定义C#方式适合动态逻辑、运行时计算、事件处理混合使用利用两者优势XAML定义基础结构C#处理动态逻辑数据绑定实现界面和数据的分离更好的MVVM模式支持1.基本外观属性Window x:ClassWpfApp.MainWindow xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:xhttp://schemas.microsoft.com/winfx/2006/xaml Title主窗体 !-- 窗体标题 -- NameMainWindow !-- 窗体名称 -- WindowStyleSingleBorderWindow !-- 窗体样式None, SingleBorderWindow, ThreeDBorderWindow, ToolWindow -- AllowsTransparencyFalse !-- 是否允许透明WindowStyleNone时才有效 -- Opacity1.0 !-- 透明度0-1之间 -- BackgroundWhite !-- 背景颜色 -- ForegroundBlack !-- 前景颜色 -- BorderBrushGray !-- 边框颜色 -- BorderThickness1 !-- 边框粗细 -- FontFamilyMicrosoft YaHei !-- 字体 -- FontSize12 !-- 字体大小 -- FontWeightNormal !-- 字体粗细 -- FontStyleNormal !-- 字体样式 -- CursorArrow !-- 鼠标指针样式 -- /Window2.大小和位置属性Window x:ClassWpfApp.MainWindow xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:xhttp://schemas.microsoft.com/winfx/2006/xaml Width800 !-- 窗体宽度 -- Height600 !-- 窗体高度 -- MinWidth400 !-- 最小宽度 -- MinHeight300 !-- 最小高度 -- MaxWidth1200 !-- 最大宽度 -- MaxHeight900 !-- 最大高度 -- WindowStartupLocationCenterScreen !-- 启动位置CenterScreen, CenterOwner, Manual -- Left100 !-- 左侧位置Manual模式有效 -- Top100 !-- 顶部位置Manual模式有效 -- SizeToContentManual !-- 大小适应内容Manual, Width, Height, WidthAndHeight -- /Window3.状态和行为属性Window x:ClassWpfApp.MainWindow xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:xhttp://schemas.microsoft.com/winfx/2006/xaml WindowStateNormal !-- 窗口状态Normal, Maximized, Minimized -- ResizeModeCanResize !-- 调整大小模式NoResize, CanMinimize, CanResize, CanResizeWithGrip -- ShowInTaskbarTrue !-- 是否在任务栏显示 -- TopmostFalse !-- 是否置顶 -- VisibilityVisible !-- 可见性Visible, Hidden, Collapsed -- IsEnabledTrue !-- 是否可用 -- FocusableTrue !-- 是否能获得焦点 -- ShowActivatedTrue !-- 显示时是否激活 -- WindowStartupLocationCenterScreen !-- 启动位置 -- IconImages/app.ico !-- 窗体图标 -- TaskbarItemInfo{Binding TaskBarInfo} !-- 任务栏缩略图信息 -- /Window4.C#代码设置属性public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); // 基本属性设置 this.Title 主窗体; this.Name MainWindow; this.Width 800; this.Height 600; this.MinWidth 400; this.MinHeight 300; this.MaxWidth 1200; this.MaxHeight 900; // 位置设置 this.WindowStartupLocation WindowStartupLocation.CenterScreen; this.Left 100; this.Top 100; // 外观设置 this.WindowStyle WindowStyle.SingleBorderWindow; this.ResizeMode ResizeMode.CanResize; this.WindowState WindowState.Normal; this.Background System.Windows.Media.Brushes.White; this.Foreground System.Windows.Media.Brushes.Black; this.Opacity 1.0; this.AllowsTransparency false; // 行为设置 this.ShowInTaskbar true; this.Topmost false; this.Visibility Visibility.Visible; this.IsEnabled true; this.Focusable true; this.ShowActivated true; // 设置图标 Uri iconUri new Uri(pack://application:,,,/Images/app.ico, UriKind.RelativeOrAbsolute); this.Icon BitmapFrame.Create(iconUri); } }