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

WPF—Style样式

WPF—Style样式

样式——就是一种将一组属性值应用到多个元素的便捷方法。

样式的类型叫Style,继承于DispatcherObject,关键属性包括:

  • TargetType:指定样式适用的控件类型(如 Button)

  • BasedOn:实现样式继承

  • Triggers:触发器集合(满足条件时动态修改样式)

  • Setters:属性设置项集合(存储控件属性的键值对)

  • Resources:资源字典(存放可复用的样式 / 资源)

一、Style写法

1、写在控件内部

样式只对该控件有效

<!--控件内部样式: 样式只对该控件有效 -->
<Button Content="按钮1"><Button.Style><Style TargetType="Button"><Setter  Property="Background" Value="Green"/><Setter  Property="Foreground" Value="White"/><Setter  Property="FontSize" Value="20"/><Setter Property="Width" Value="100"/><Setter Property="Height" Value="50"/><Setter Property="Margin" Value="5" /></Style></Button.Style>
</Button>

效果

image

2、写在窗体文件

对当前窗体有效

2.1、不指定KeyStyle

 <!--窗体资源-->
<Window.Resources><!-- 根据类型匹配, 适应所有Button--><Style TargetType="Button"><Setter  Property="Background" Value="Green"/><Setter  Property="Foreground" Value="White"/><Setter  Property="FontSize" Value="20"/><Setter Property="Width" Value="100"/><Setter Property="Height" Value="50"/><Setter Property="Margin" Value="5"/></Style>
</Window.Resources>
<Button Content="按钮1" Style="{StaticResource {x:Type Button}}">
</Button>

效果

image

2.2、指定keyStyle

<Window.Resources><!--指定key的Style--><Style TargetType="Button" x:Key="RedButtonStyle"><Setter  Property="Background" Value="Red"/><Setter  Property="Foreground" Value="White"/><Setter  Property="FontSize" Value="20"/><Setter Property="Width" Value="100"/><Setter Property="Height" Value="50"/><Setter Property="Margin" Value="5"/></Style>
</Window.Resources>
<Grid><StackPanel Orientation="Horizontal"><Button Content="按钮1"></Button><Button Content="按钮2" Style="{StaticResource RedButtonStyle}"></Button></StackPanel>
</Grid>

效果

image

3、样式继承(BasedOn

样式支持 “基样式 + 扩展样式” 的继承模式,基样式定义通用属性,扩展样式仅覆盖差异化属性,减少重复代码。

<!--窗体资源-->
<Window.Resources><Style TargetType="Button" x:Key="BaseButtonStyle"><Setter  Property="Foreground" Value="White"/><Setter  Property="FontSize" Value="20"/><Setter Property="Width" Value="100"/><Setter Property="Height" Value="50"/><Setter Property="Margin" Value="5"/></Style><!--指定key的Style--><Style TargetType="Button" x:Key="RedButtonStyle" BasedOn="{StaticResource BaseButtonStyle}"><Setter  Property="Background" Value="SkyBlue"/></Style>
</Window.Resources>
<Grid><StackPanel Orientation="Horizontal"><Button Content="按钮1"></Button><Button Content="按钮2" Style="{StaticResource RedButtonStyle}"></Button></StackPanel>
</Grid>

效果

image

可以发现,为什么第一个按钮没有使用基本样式?因为 x:Key 的样式,不会自动应用到控件上

WPF 样式的两条铁律

  • 只有 TargetType,没有 x:Key,✅ 会自动应用到页面上所有同类型控件(所有 Button 自动生效)。

  • 既有 TargetType,又有 x:Key,❌ 不会自动应用,必须手动写 Style="{StaticResource Key}" 才会生效。

4、写在字典资源文件中

在整个项目的窗体有效

第一步、创建资源字典

image

第二步、把Style写在资源字典文件中(笔刷、模板都可以作为一种资源写在资源字典文件中)

<!--画笔 --><SolidColorBrush x:Key="ButtonBackground" Color="Blue"/><SolidColorBrush x:Key="ButtonForeground" Color="White"/><Style TargetType="Button" x:Key="BaseButtonStyle"><Setter  Property="Foreground" Value="{StaticResource ButtonForeground}"/><Setter  Property="FontSize" Value="20"/><Setter Property="Width" Value="100"/><Setter Property="Height" Value="50"/><Setter Property="Margin" Value="5"/></Style><!--指定key的Style--><Style TargetType="Button" x:Key="RedButtonStyle" BasedOn="{StaticResource BaseButtonStyle}"><Setter  Property="Background" Value="Red"/></Style><Style TargetType="Button" x:Key="GreenButtonStyle" BasedOn="{StaticResource BaseButtonStyle}"><Setter  Property="Background" Value="Green"/></Style>

如图:

image

第三步、在App.xaml文件中的 Application.Resources 中添加资源字典文件

<Application.Resources><!--创建一个ResourceDictionary对象--><ResourceDictionary><Style x:Key="TextBoxStyle" TargetType="TextBox"><Setter Property="Width" Value="200"/></Style><!--包含其他资源字典,使用MergedDictionaries属性添加 --><ResourceDictionary.MergedDictionaries><!--Source: 字典资源文件相对路径--><ResourceDictionary Source="/Dictionary1.xaml"></ResourceDictionary><!--其他的字典资源文件--></ResourceDictionary.MergedDictionaries></ResourceDictionary>
</Application.Resources>

如图:

image

第四步、直接使用

<Grid><StackPanel Orientation="Horizontal"><Button Content="按钮1" Style="{StaticResource BaseButtonStyle}"></Button><Button Content="按钮2" Style="{StaticResource GreenButtonStyle}"></Button><Button Content="按钮3" Style="{StaticResource RedButtonStyle}"></Button></StackPanel>
</Grid>

效果

image

二、Style触发器(Trigger)

Trigger触发器的四个属性:

  • Property:监听哪个属性(必须依赖属性)

  • Value:属性等于什么值时触发

  • Setters:触发后修改哪些属性

  • SourceName:监听哪个控件的属性(默认自己)

需要注意的是:Trigger触发器的条件应该是当前控件拥有的属性名称

使用

在样式中添加:

<Style TargetType="Button" x:Key="GreenButtonStyle" BasedOn="{StaticResource BaseButtonStyle}"><Setter  Property="Background" Value="Green"/><Style.Triggers><!--使用触发器,实现鼠标移入时样式--><Trigger Property="IsMouseOver" Value="True"><!--设置鼠标移入时, 样式属性--><Setter Property="Background" Value="Orange"/><Setter Property="Foreground" Value="Red"/><Setter Property="Width" Value="200"/><Setter Property="Height" Value="100"/><Setter Property="FontSize" Value="30"/></Trigger></Style.Triggers></Style>

如图:

image

效果

csbbVd8F_converted

可以发现,我们虽然增加了鼠标移入设置背景颜色,但是为什么背景颜色为什么没有改变呢?因为Button 的外观不是由 Background 属性直接渲染的,而是由 ControlTemplate(控件模板)画出来的!

http://www.jsqmd.com/news/668018/

相关文章:

  • CREST:分子构象采样的终极指南,快速探索化学空间
  • STM32 FSMC驱动TFTLCD:从点阵到任意尺寸字体的高效显示方案
  • Windows 10专业版用户必看:用组策略彻底关掉Defender的保姆级教程(附防篡改设置)
  • mysql数据量过亿时索引如何优化_mysql分库分表索引设计
  • 联想小新Air14 AMD版装Ubuntu 20.04,升级内核到5.11解决触控板和亮度问题(附详细步骤)
  • Bootstrap Gutters间距用法 Bootstrap 5中g-,gx-,gy--如何使用
  • 2026届最火的五大降重复率助手推荐
  • Nacos2.x核心源码深度剖析:从通信到业务
  • 股票行情核心指标与形态解析
  • winodws下cpolar 公网穿透保姆级安装使用教程
  • 2026电压力锅哪个牌子质量好?高口碑品牌推荐 - 品牌排行榜
  • 告别虚拟机!在Win11的WSL2里从源码编译安装Madagascar(保姆级避坑指南)
  • Nexys A7 实战入门:从流水灯到硬件描述语言
  • Chrome DevTools MCP:让 AI 编码助手拥有浏览器调试超能力
  • 2026最权威的十大降重复率助手推荐
  • 从共享单车需求预测看ST-Norm:为什么你的时序模型总忽略局部特征?
  • 告别Three.js!用3Dmol.js在Web端5分钟搞定分子3D可视化(附完整代码)
  • java的学习之路
  • Rust的匹配中的进展编译器
  • HDMI 2.1高速信号PCB设计避坑指南:从4层板布线到SI仿真验证
  • 告别ArcGIS依赖:用Python+GDAL的OpenFileGDB驱动,5分钟搞定GDB数据读取
  • OriginPro 2023保姆级教程:用自带示例数据5步搞定带正态分布曲线的多因子分组箱线图
  • 从RobotStudio到Eigen库:手把手教你用C++验证ABB机器人正逆解(IRB 1600-6/1.45型号)
  • COMSOL模拟环偶极子增强磁光克尔效应
  • 从‘有状态’到实战:用iptables为你的Ubuntu服务器打造企业级安全策略
  • 50元搞定远程开机:米家智能插座+BIOS设置保姆级教程(附休眠模式小技巧)
  • 别再只会插上就用了!手把手教你用V4L2在Ubuntu上精细调校USB摄像头(亮度/曝光/白平衡)
  • Wand-Enhancer:零成本解锁WeMod高级功能的终极指南
  • WeChatExporter:微信聊天记录数据提取与结构化备份技术方案
  • 从STC8G1K08A到SG90舵机:一个宿舍断电关灯器的硬件选型与避坑全记录