WPF 学习笔记

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>

控件模板

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学习笔记-控件之间绑定

https://www.cnblogs.com/anyihen/p/12926655.html

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值