WPF 应用程序组成
-
app.config 配置文件 连接字符 配置信息
-
App.xaml 设置应用程序起始文件,系统级资源
-
App.xam.cs app.xaml文件的后台类文件
-
MainWindow.xaml Windows–WPF应用程序界面与Xaml设计文件
-
MainWindow.xaml.cs –xaml窗口文件的后台代码文件
xaml
StartupUri="MainWindow.xaml" :指定起始文件
<Application.Resources> 定义整个WPF应用程序的相关资源(全局配置)
一个xaml文件至少要有两个命名空间 1.默认命名空间 2.带x前缀
常用配置
xaml
<Window x:Class="WpfTest.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:WpfTest"
mc:Ignorable="d"
Title="标题" Height="300" Width="800" ShowInTaskbar="False">
ShowInTaskbar:窗口打开是否在任务栏显示
WindowStartupLocation:窗口默认显示在屏幕中间
WindowState="Maximized":窗口默认最大化
Topmost="True" 窗口永远在其他窗口最上层
常用标签
Label:文本标签(内容控件)、TextBox:文本框(控件)、PasswordBox:密码框、Button:按钮(内容控件)
WPF允许控件没有Name属性,后台代码需要引用对象,需要设置Name
TextBox 内容:Text、是否换行:TextWrapping
Content:文本、Margin:位置、HorizontalAlignment:左右对齐、VerticalAlignment:上下对齐、Width:宽度
样式
xaml
<Window.Resources>
<Style x:Key="defaultStyle" TargetType="Button">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="FountSize" Value="30"/>
</Style>
</Window.Resources>
<!--Style引用-->
<Button Style="{StaticResource defaultStyle}" Content="按钮文本" />
<!--style 继承-->
<Window.Resources>
<Style x:Key="baseButtonStyle" TargetType="Button">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="FountSize" Value="30"/>
</Style>
<Style x:Key="defaultStyle" TargetType="Button" BaseOn="{StaticResource baseButtonStyle}">
<Setter Property="FountSize" Value="20"/>
</Style>
</Window.Resources>
触发器
单条件触发器
实现功能:鼠标放到控件上,字体颜色与大小发生变化
xaml
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Fountground" Value="Red"/>
<Setter Property="FountSize" Value="30"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Fountground" Value="Blue"/>
<Setter Property="FountSize" Value="20"/>
</Trigger>
</Style.Triggers>
多条件触发器
xaml
<Style.Triggers>
<MultiTrigger>
<!--多条件-->
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True"></Condition>
<Condition Property="IsFocused" Value="True"></Condition>
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter Property="Fountground" Value="Red"/>
</MultiTrigger.Setters>
</MultiTrigger>
</Style.Triggers>
事件触发器
实现功能,鼠标悬浮,控件字体大小动态变化
xaml
<Style.Triggers>
<EventTrigger RoutedEvent="Mouse.MouseEnter">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="0:0:0.2"
Storyboard.TargetProperty="FontSize"
To="20">
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
控件模板
![](https://img-blog.csdnimg.cn/img_convert/65f95515e49f3b46681eb7c654189ef3.png)
image-20210307144830302
xaml
<Window.Resources>
<ControlTemplate x:Key="defaultButtonTemplate" TargetType="Button">
<Border Background="Red" CornerRadius="5">
<StackPanel Orientation="Horizontal" HorizontalAlignment="{TemplateBinding HorizontalAlignment}">
<TextBlock Text="❤" VerticalAlignment="Center"/>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
VerticalAlignment="{TemplateBinding VerticalAlignment}"/>
</StackPanel>
</Border>
</ControlTemplate>
</Window.Resources>
<Button Template="{StaticResource defaultButtonTemplate}"
HorizontalAlignment="Center" VerticalAlignment="Center"
Height="40"/>
https://blog.csdn.net/u010839204/category_9916743.html 很多
WPF学习笔记-CSDN博客 很多!!!
https://www.cnblogs.com/anyihen/category/1767344.html
https://www.cnblogs.com/HarryK4952/category/1933179.html WPF 10天修炼
WPF 样式, 资源, 触发器, 行为, 事件,绑定Binding, 模板,
WPF资源、样式、控件模板
https://www.cnblogs.com/HarryK4952/p/14445343.html
WPF学习笔记-控件之间绑定