tnblog
首页
视频
资源
登录

WPF基础学习笔记(二)

836人阅读 2025/1/8 17:39 总访问:3659944 评论:0 收藏:0 手机
分类: .net后台框架

.netcore

WPF基础学习笔记(二)

设置控件样式


在 WPF 中,Setter 是用来定义控件样式(Style)的一个重要元素。
它允许你为控件的某些属性指定值,比如背景色、字体、边框等。Setter 会根据控件的状态或特性,自动应用指定的样式。

Setter 的基本用法


Setter 用于 Style 中,用来为目标控件的特定属性设置值。每个 Setter 需要指定两个属性:
1.Property:需要设置的属性,例如 BackgroundFontSize 等。
2.Value:设置该属性的值,可以是常量值(如 Red16)或者引用资源(如 StaticResourceDynamicResource)。

举例:

  1. <Window x:Class="WpfAppLearning.StyleWin"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:local="clr-namespace:WpfAppLearning"
  7. mc:Ignorable="d"
  8. Title="StyleWin" Height="450" Width="800">
  9. <Window.Resources>
  10. <!-- 定义一个按钮样式 -->
  11. <Style x:Key="MyButtonStyle" TargetType="Button">
  12. <!-- 设置按钮的背景颜色 -->
  13. <Setter Property="Background" Value="SkyBlue"/>
  14. <!-- 设置按钮的字体大小 -->
  15. <Setter Property="FontSize" Value="16"/>
  16. <!-- 设置按钮的文本颜色 -->
  17. <Setter Property="Foreground" Value="White"/>
  18. <!-- 设置按钮的边框 -->
  19. <Setter Property="BorderBrush" Value="DarkSlateGray"/>
  20. <Setter Property="BorderThickness" Value="2"/>
  21. </Style>
  22. </Window.Resources>
  23. <Grid>
  24. <!-- 应用样式到按钮 -->
  25. <Button Content="Click Me" Style="{StaticResource MyButtonStyle}" Width="200" Height="50" />
  26. </Grid>
  27. </Window>

Triggers样式触发器


Style.Triggers触发器允许你根据控件的某些属性或状态来改变控件的外观。
这里举个简单的例子通过当一个选项框为True时背景色为绿色,当一个选项框为False时背景色为红色。

  1. <Window x:Class="WpfAppLearning.StyleWin"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:local="clr-namespace:WpfAppLearning"
  7. mc:Ignorable="d"
  8. Title="StyleWin" Height="450" Width="800">
  9. <Window.Resources>
  10. <Style TargetType="CheckBox" x:Key="checkboxStyle">
  11. <Setter Property="Background" Value="Red"></Setter>
  12. <Style.Triggers>
  13. <Trigger Property="IsChecked" Value="True">
  14. <Setter Property="Background" Value="Green"></Setter>
  15. </Trigger>
  16. </Style.Triggers>
  17. </Style>
  18. </Window.Resources>
  19. <Grid>
  20. <CheckBox Content="Hmy" Style="{StaticResource checkboxStyle}" IsChecked="True"></CheckBox>
  21. </Grid>
  22. </Window>

MultiTrigger多条件触发器


MultiTrigger用于检查多个属性的状态,并且只有所有条件都满足时才触发。
这里我举个例子,当CheckBoxFalse宽度为100时触发。

  1. <Window x:Class="WpfAppLearning.StyleWin"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:local="clr-namespace:WpfAppLearning"
  7. mc:Ignorable="d"
  8. Title="StyleWin" Height="450" Width="800">
  9. <Window.Resources>
  10. <Style TargetType="CheckBox" x:Key="checkboxStyle">
  11. <Setter Property="Background" Value="Red"></Setter>
  12. <Style.Triggers>
  13. <Trigger Property="IsChecked" Value="True">
  14. <Setter Property="Background" Value="Green"></Setter>
  15. </Trigger>
  16. <MultiTrigger>
  17. <MultiTrigger.Conditions>
  18. <Condition Property="IsChecked" Value="False"/>
  19. <Condition Property="Width" Value="100"/>
  20. </MultiTrigger.Conditions>
  21. <Setter Property="Background" Value="Blue"></Setter>
  22. </MultiTrigger>
  23. </Style.Triggers>
  24. </Style>
  25. </Window.Resources>
  26. <Grid>
  27. <CheckBox Content="Hmy" Style="{StaticResource checkboxStyle}" IsChecked="True" Width="100"></CheckBox>
  28. </Grid>
  29. </Window>


下面使用Trigger通过监控IsPressed属性是否点击Button按钮来触发将宽度增加到300

  1. <Window x:Class="WpfAppLearning.StyleWin"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:local="clr-namespace:WpfAppLearning"
  7. mc:Ignorable="d"
  8. Title="StyleWin" Height="450" Width="800">
  9. <Grid>
  10. <Button Content="Click Me" Width="100" Height="100">
  11. <Button.Style>
  12. <Style TargetType="Button">
  13. <Style.Triggers>
  14. <Trigger Property="IsPressed" Value="True">
  15. <Trigger.EnterActions>
  16. <BeginStoryboard>
  17. <Storyboard>
  18. <DoubleAnimation Storyboard.TargetProperty="Width"
  19. To="300"
  20. Duration="0:0:0.5"/>
  21. </Storyboard>
  22. </BeginStoryboard>
  23. </Trigger.EnterActions>
  24. </Trigger>
  25. </Style.Triggers>
  26. </Style>
  27. </Button.Style>
  28. </Button>
  29. </Grid>
  30. </Window>

这里需要大家亲自尝试一下才能感觉出来。
然后下面同样通过监听Button.Click事件来进行触发:

  1. <Window x:Class="WpfAppLearning.StyleWin"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:local="clr-namespace:WpfAppLearning"
  7. mc:Ignorable="d"
  8. Title="StyleWin" Height="450" Width="800">
  9. <Grid>
  10. <Button Content="Click Me" Width="100" Height="100">
  11. <Button.Triggers>
  12. <EventTrigger RoutedEvent="Button.Click">
  13. <BeginStoryboard>
  14. <Storyboard>
  15. <DoubleAnimation Storyboard.TargetProperty="Width"
  16. To="300"
  17. Duration="0:0:0.5"/>
  18. </Storyboard>
  19. </BeginStoryboard>
  20. </EventTrigger>
  21. </Button.Triggers>
  22. </Button>
  23. </Grid>
  24. </Window>


这里动画标签DoubleAnimation其中的属性如下表所示:

属性名 类型 说明
Storyboard.TargetProperty string 指定动画所影响的目标属性(如 WidthHeightOpacity)。
To double 动画的目标值,即动画最终会变成的值。
From double(可选) 动画的起始值,默认为当前值。如果未指定,动画会从当前状态开始变化。
By double(可选) 动画的增量值,与起始值叠加计算目标值(与 To 互斥)。
Duration TimeSpan 动画持续时间,例如 0:0:0.5 表示持续 0.5 秒。
AutoReverse bool 指定动画结束后是否反向播放,默认为 false
RepeatBehavior RepeatBehavior 控制动画重复行为,可指定次数(如 3x)或为 Forever(无限循环)。
AccelerationRatio double 指定动画加速的比例(0-1),控制动画起始阶段的加速效果。
DecelerationRatio double 指定动画减速的比例(0-1),控制动画结束阶段的减速效果。
EasingFunction IEasingFunction 用于设置动画的缓动函数,如平滑、弹性、回弹等效果。
FillBehavior FillBehavior 指定动画结束后是否保留最终值,可设置为 HoldEndStop

欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739

评价

WPF DataGrid表头Checkbox 全选与全反选

WPF 在使用DataGrid展示数据的时候经常会使用到checkbox列,特别是id列 例如下面这种效果:WPF 要实现DataGrid checkbox全...

WPF基础学习笔记

WPF基础学习笔记[TOC] WPF简介在WPF(Windows Presentation Foundation)中,布局控件用于管理和组织界面元素的排列与尺寸...

WPF渲染模板

WPF渲染模板[TOC] 简介在WPF(Windows Presentation Foundation)中,渲染模板是一种强大的机制,允许开发者定义控件的外...

WPF CommunityToolkit.Mvvm初探

WPF CommunityToolkit.Mvvm初探[TOC] 什么是 CommunityToolkit.Mvvm?CommunityToolkit.Mvvm 是一个现代化的 MVVM 框架,...

WPF Mvvmlight初探

WPF Mvvmlight初探[TOC] 什么是 Mvvmlight?MVVM Light 是一个轻量级的 MVVM 框架,适用于 WPF、UWP、Xamarin 等多个平台...

WPF MvvmLight Messager学习

WPF MvvmLight Messager学习[TOC] NotificationMessage在 MVVM Light 中,NotificationMessage 是一种消息类型,用于在 Vi...

WPF Prism 框架:打造高效、可维护的 WPF 应用

WPF Prism 框架:打造高效、可维护的 WPF 应用[TOC] Prism 框架简介Prism 是一个用于构建松耦合、可维护且可测试的 XAML ...

WPF Prism 框架初始化

WPF Prism 框架初始化[TOC] 什么是 Prism Bootstrapper?Prism Bootstrapper 是一个抽象类,它定义了一个基本的启动序列,...

WPF Prism ViewModel的应用

WPF Prism ViewModel的应用[TOC] 在 WPF 开发中,Prism 是一个非常流行的框架,它基于 MVVM(Model-View-ViewModel)模式...

WPF Prism Dialog与Region

WPF Prism Dialog与Region[TOC] Prism框架中的Dialog子窗口处理在 WPF 应用程序开发中,对话框是一个常见的功能需求,无论...

WPF Prism 复合命令与模块化管理

WPF Prism 复合命令与模块化管理[TOC] Prism 中的 CompositeCommand 示例CompositeCommand 是 Prism 框架中用于组合多个子...

C ?、?? 问号和2个问号的用法类型?、对象?

C# ?C# ???:单问号1.定义数据类型可为空。可用于对int,double,bool等无法直接赋值为null的数据类型进行null的赋值如这...

Python实例 1-日志抓取处理 补错附日志小技巧

有时候数据出了问题,可以从日志中恢复数据(如果你没记日志..没备份..→_→..)一、日志展示介绍个平常自己用的小方法,如...

C 数组拆分泛型

主要用到了泛型。泛型是c#2.0的一个新增加的特性,它为使用c#语言编写面向对象程序增加了极大的效力和灵活性。不会强行对值...

MySQL 视图的增删改 查

要显示视图的定义,需要在SHOWCREATEVIEW子句之后指定视图的名称, 我们先来创建几张表,完事后在进行演示:--用户信息表...
这一世以无限游戏为使命!
排名
2
文章
657
粉丝
44
评论
93
docker中Sware集群与service
尘叶心繁 : 想学呀!我教你呀
一个bug让程序员走上法庭 索赔金额达400亿日元
叼着奶瓶逛酒吧 : 所以说做程序员也要懂点法律知识
.net core 塑形资源
剑轩 : 收藏收藏
映射AutoMapper
剑轩 : 好是好,这个对效率影响大不大哇,效率高不高
ASP.NET Core 服务注册生命周期
剑轩 : http://www.tnblog.net/aojiancc2/article/details/167
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术