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

WPF学习笔记(17)样式Style

样式Style

  • 1. 概述
  • 2 Style详解
  • 3. Setter详解
  • 4 Style用法
  • 5. EventSetter详解
  • 6 EventSetterStyle用法
  • 总结


1. 概述

样式(Style类)用于给控件定义外观,样式包含一个或多个 Setter对象的集合,每个 Setter由 Property和 Value组成。
样式也是一种资源,可以像引用任何其他资源一样对其进行引用。
官方文档:https://learn.microsoft.com/zh-cn/dotnet/api/system.windows.style?view=netframework-4.8

2 Style详解

Style类提供一个WPF资源对象,其部分属性如下:

属性说 明
TargetType获取或设置此样式所针对的类型。
BasedOn获取或设置一个作为当前样式的基准的已定义样式
Setters获取 SetterEventSetter 对象的集合,
Triggers获取基于指定条件应用属性值的 TriggerBase 对象的集合.。
Resources获取或设置可在此样式的范围内使用的资源的集合。

3. Setter详解

Setter提供一个设置属性的值。

属性说 明
Property获取或设置要应用 Value 的属性。
Value获取或设置属性的值。
TargetName获取或设置此 Setter 所用于的元素x:Name的名称。仅在 ControlTemplate的Triggers中的setter生效

4 Style用法

在这里插入图片描述
在这里插入图片描述

  • 需注意,Style的TargetType需为实际的对象控件类,如果设置为父类,则对其子类不生效。
  • 控件想使用父类的Style时,需在Style中添加 x:Key=“myStyle” ,并在控件的描述中添加资源引用StaticRecource或者DynamicRecource。StaticRecource在编译时确定值,DynamicRecource在运行时动态计算值。
  • 当既有父类TargetType,又有该类对象控件类时,资源引用StaticRecource或者DynamicRecource
    优先级更高,使用本对象的Style时,资源引用可省略。在这里插入图片描述
    在Style的定义中,还可以继承之前的样式。
    在这里插入图片描述
    示例代码与运行效果如下;
   <Window.Resources><!--设置父类Style--><Style x:Key="myKey" TargetType="Control"><Setter Property="Background" Value="Red"/><Setter Property="Foreground" Value="White"/><Setter Property="FontSize" Value="20"/></Style><Style TargetType="TextBox"><Setter Property="BorderBrush" Value="blue"/><Setter Property="BorderThickness" Value="4"/><Setter Property="FontSize" Value="15"/></Style><!--继承自父类Control的Style--><Style x:Key="myKey2" TargetType="TextBox" BasedOn="{StaticResource myKey}"><Setter Property="BorderBrush" Value="Black"/><Setter Property="BorderThickness" Value="4"/><Setter Property="FontSize" Value="22"/></Style><!--StaticResource {x:Type TextBox}继承的为第一次设置的Style,无x:Key设置时的情况--><Style x:Key="myKey3" TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}"><Setter Property="BorderBrush" Value="GreenYellow"/><Setter Property="Background" Value="Yellow"/></Style></Window.Resources><Grid><Button x:Name="button1" Content="Button" Style="{StaticResource myKey}" HorizontalAlignment="Left" Margin="198,69,0,0" VerticalAlignment="Top" Height="40" Width="120"/><Button x:Name="button2" Content="Button" Style="{DynamicResource myKey}" HorizontalAlignment="Left" Margin="198,159,0,0" VerticalAlignment="Top" Height="40" Width="120"/><Button x:Name="button3" Content="Button" HorizontalAlignment="Left" Margin="198,249,0,0" VerticalAlignment="Top" Height="40" Width="120"/><TextBox x:Name="textBox1" Style="{StaticResource myKey}" TextAlignment="Center" HorizontalAlignment="Left" Margin="400,69,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" Height="40"/><TextBox x:Name="textBox2" Style="{StaticResource myKey2}" HorizontalAlignment="Left" TextAlignment="Center" Margin="400,159,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" Height="40"/><TextBox x:Name="textBox3" Style="{StaticResource myKey3}" HorizontalAlignment="Left" Margin="400,249,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" Height="40"/><TextBox x:Name="textBox4" TextAlignment="Center" HorizontalAlignment="Left" Margin="608,69,0,0" TextWrapping="Wrap" Text="TextBox4" VerticalAlignment="Top" Width="120" Height="40"/></Grid>

在这里插入图片描述

5. EventSetter详解

EventSetter 提供一个设置事件的值。

属性说 明
Event获取或设置此 EventSetter 响应的特定路由事件。
Handler获取或设置对资源库中路由事件的处理程序的引用。

6 EventSetterStyle用法

在这里插入图片描述
代码与输出结果如下

    <Window.Resources><Style TargetType="Button"><EventSetter Event="Click" Handler="Button_Click"/><EventSetter Event="MouseEnter" Handler="Button_MouseEnter"/></Style><Style TargetType="TextBox"><EventSetter Event="TextChanged" Handler="TextBox_TextChanged"/></Style></Window.Resources><Grid><Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="284,69,0,0" VerticalAlignment="Top" Height="88" Width="247"/><TextBox x:Name="textBox" HorizontalAlignment="Left" Height="112" Margin="284,201,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="247"/></Grid>
        private void Button_Click(object sender, RoutedEventArgs e){MessageBox.Show("按钮被点击");}private void Button_MouseEnter(object sender, MouseEventArgs e){MessageBox.Show("鼠标移入");}private void TextBox_TextChanged(object sender, TextChangedEventArgs e){TextBox tb = (TextBox)sender;Console.WriteLine("文件框内容为:" + tb.Text);}

在这里插入图片描述


总结

  • Style中可以通过Setter和EventSetter设置统一的样式属性和事件
  • Style中的样式可通过BasedOn继承
http://www.lqws.cn/news/590473.html

相关文章:

  • Coze(扣子):基础学习
  • 利用视觉-语言模型搭建机器人灵巧操作的支架
  • 【Docker基础】Docker数据卷:数据卷的作用与使用场景
  • 算法-每日一题(DAY12)最长和谐子序列
  • Salesforce Accountアクションボタン実装ガイド
  • 简单聊聊 Flutter 在鸿蒙上为什么可以 hotload ?
  • 飞算JavaAI—AI编程助手 | 编程领域的‘高科技指南针’,精准导航开发!
  • 具身多模态大模型在感知与交互方面的综述
  • sqlmap学习ing(2.[第一章 web入门]SQL注入-2(报错,时间,布尔))
  • rocketmq 之 阿里云转本地部署实践总结
  • Vue3 中 Excel 导出的性能优化与实战指南
  • 创建和连接Vue应用程序实例
  • 缓存系统-淘汰策略
  • 强化学习系列--dpo损失函数
  • 齿轮的齿厚极限偏差如何确定?一起学习一下
  • C++基础
  • 目前最火的agent方向-A2A快速实战构建(二): AutoGen模型集成指南:从OpenAI到本地部署的全场景LLM解决方案
  • 《Python 架构之美:三大设计模式实战指南》
  • 【FR801xH】富芮坤FR801xH之UART
  • 【javaAI】SpringAI快速入门
  • 【C#】如果有一个数值如 168.0000100,如何去除末尾的无效零,只显示有效的小数位数,让DeepSeek给我们解答
  • 半加器和全加器
  • Disruptor架构哲学
  • 【机器学习2】正则化regularizaiton(降低模型过拟合)
  • 设备管理的11个指标、七大误区、六大特征
  • muduo
  • 数据结构——线性表的链式存储
  • QT笔记---环境和编译出现的问题
  • Golang的代码结构设计原则与实践与模式应用
  • helm安装配置jenkins