当前位置: 首页 > backend >正文

wpf之样式

前言

wpf的style样式用于将一组属性设置封装起来放置于资源文件中,从而轻松地应用到多个元素上,从而实现 UI 的统一和换肤功能,style主要有Setter、Trigger两个特性,

1、Setter

下面的代码中使用style通过Setter封装了三个属性Background、Foreground、FontSize,让这三个属性成为一个样式,并且通过TargetType="Button"来指定Button控件使用这个样式,并且使用x:Key=“MyButtonStyle"来为这个样式起了一个名称,并且这个样式放置于Window.Resources中使样式成为了资源文件。最后使用Button时,通过Style=”{StaticResource MyButtonStyle}"来让Button使用指定的样式.

<Window x:Class="wpf之Style.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:wpf之Style"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Window.Resources><Style x:Key="MyButtonStyle" TargetType="Button"><Setter Property="Background" Value="Red" /><Setter Property="Foreground" Value="White"/><Setter Property="FontSize" Value="25"/></Style></Window.Resources><Grid><StackPanel ><Button  Height=" 100" Style="{StaticResource MyButtonStyle}"  Content="样式Button"/><Button  Height=" 100" Content="普通Button" /></StackPanel ></Grid>
</Window>

2、运行结果

从运行结果可以看出,使用了样式的Button,背景色、字体颜色、字体大小和不使用样式的Button比都得到了改变。
在这里插入图片描述

2、Trigger

Trigger称为触发器,Property是触发器关注的属性,Value是触发的条件,下面的代码中使用了两个触发器,当CheckBox的IsChecked为true时,背景色设置为Green,否则设置为红色

<Window x:Class="wpf之Style.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:wpf之Style"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Window.Resources><Style x:Key="MyCheckBoxStyle" TargetType="CheckBox" ><Style.Triggers><Trigger Property="IsChecked" Value="True" ><Setter Property="Background" Value="Green"    /></Trigger><Trigger Property="IsChecked" Value="False"  ><Setter Property="Background" Value="Red"  /></Trigger></Style.Triggers></Style></Window.Resources><Grid><StackPanel Orientation="Vertical" ><CheckBox   Style="{StaticResource MyCheckBoxStyle}"  Content="样式CheckBox"/><CheckBox   Content="普通CheckBox" /></StackPanel ></Grid>
</Window>

1)IsChecked为true
在这里插入图片描述

2)IsChecked为false
在这里插入图片描述

马工撰写的年入30万+C#上位机项目实战必备教程(点击下方链接即可访问文章目录)

1、《C#串口通信从入门到精通》
2、《C#与PLC通信从入门到精通 》
3、《C# Modbus通信从入门到精通》
4、《C#Socket通信从入门到精通 》
5、《C# MES通信从入门到精通》
6、《winform控件从入门到精通》
7、《C#操作MySql数据库从入门到精通》

http://www.xdnf.cn/news/19507.html

相关文章:

  • PAT 1089 Insert or Merge
  • UBUNTU之Onvif开源服务器onvif_srvd:1、编译
  • 如何使用VMware创建一台Ubuntu机器
  • Shell脚本实用技巧集锦:从时间判断到系统监控
  • 【数据可视化-104】安徽省2025年上半年GDP数据可视化分析:用Python和Pyecharts打造炫酷大屏
  • HTTP/2 多路复用
  • 网络流量分析——熟悉Wireshark
  • 时序数据库国产的有哪些?
  • ​​--flush-logs 的作用:刷新 MySQL 的日志文件(主要是二进制日志 binlog)​
  • 解析简历重难点与面试回答要点
  • 【开题答辩全过程】以 健身爱好者饮食管理小程序为例,包含答辩的问题和答案
  • LeetCode82删除排序链表中的重复元素 II
  • 力扣hot100 | 堆 | 215. 数组中的第K个最大元素、347. 前 K 个高频元素、128. 最长连续序列
  • 《架构师手记:SpringCloud整合Nacos实战·一》
  • Transformer的并行计算与长序列处理瓶颈总结
  • 可编辑115页PPT | 某纸制品制造企业数字化转型战略规划项目建议书
  • 【实验protocol分享】了解流式细胞术
  • 串口通讯个人见解
  • 软考中级【网络工程师】第6版教材 第4章 无线通信网 (下)
  • matlab扫雷小游戏
  • 艾莉丝努力练剑的创作纪念日:星河初启,牧梦长空
  • 企业级主流日志系统架构对比ELKK Stack -Grafana Stack
  • pyside6小项目:进制转换器
  • 【OpenFeign】基础使用
  • Kubernetes一网络组件概述
  • Java比较器
  • 如何在 vscode 上用 git 将项目 push 到远程仓库 and 常用Git 命令
  • 剧本杀小程序系统开发:重塑社交娱乐新生态
  • 【开题答辩全过程】以 基于Spring Boot的房屋租赁系统的设计与实现为例,包含答辩的问题和答案
  • 神经网络1——sklearn的简单实现