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

WPF入门--多种方式设置样式(Style)

前言

在上篇文章中,介绍了WPF九种布局方式。本篇文章通过多种方式设置样式(Style)以控制UI元素的外观和行为。下面来具体介绍一下。

传送门

WPF入门--常用布局方式

目录

前言

一、直接在XAML中设置属性(内联样式)

二、基于特定控件类型设置属性(隐式样式)

三、基于x:Key设置属性(显式样式)

四、继承样式设置属性

五、使用应用级别的资源

六、使用资源字典(Resource Dictionary)

七、使用触发器(Triggers)

总结


一、直接在XAML中设置属性(内联样式

这是最简单的方式,直接在XAML文件中为每个控件单独设置属性。有点类似我们在HTML标签上加style属性。

<Button Width="100" Height="50" Background="Aqua" FontSize="20" x:Name="ClickMe"  Content="点击"/>

我们平时肯定很少这样写,只是偶尔标签会单独定义样式。

二、基于特定控件类型设置属性(隐式样式

直接为特定类型的所有控件应用样式。

名称描述
TargetType目标类型
Setter

样式项,键值对

Property 属性名称

Value 属性值

<Window x:Class="WpfApp1.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:WpfApp1"mc:Ignorable="d"Title="GridDemo" Height="300" Width="450"><Window.Resources><Style TargetType="Button"><Setter Property="Width" Value="100"/><Setter Property="Height" Value="50"/><Setter Property="Background" Value="Crimson"/><Setter Property="FontSize" Value="18"></Setter></Style></Window.Resources><Grid ShowGridLines="True"><Grid.RowDefinitions><RowDefinition></RowDefinition><RowDefinition></RowDefinition></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition></ColumnDefinition><ColumnDefinition></ColumnDefinition></Grid.ColumnDefinitions><Button Grid.Row="0" Grid.Column="0">Button 1</Button><Button Grid.Row="0" Grid.Column="1">Button 2</Button></Grid>
</Window>

有点类似CSS中,直接定义标签样式。

input{width: 150px;
}

这里说下  

  • <Window.Resources></Window.Resources>:是XAML中的一个元素,用于在特定的窗口范围内定义资源。资源可以是样式(Style)、模板(Template)、画笔、字符串、图像等。这些资源可以在窗口内的所有子元素中被引用和使用。
  • StaticResource和DynamicResourceStaticResource在加载XAML时(编译时)进行一次性查找和解析。这意味着在使用StaticResource引用资源时,资源的值在应用启动时就已经确定,并且在应用运行期间不会再改变。DynamicResource在运行时进行查找和解析。这意味着在使用DynamicResource引用资源时,资源的值可以在应用运行期间动态改变,并且控件会自动更新以反映资源值的变化。

这样写,也还有一个缺点,无法让每个按钮都有不同的样式。

三、基于x:Key设置属性(显式样式

x:Key有点类似CSS中的class功能,可以定义key,让不同标签引用。和第二个方式差不多,只是多了一个x:Key属性。

<Window x:Class="WpfApp1.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:WpfApp1"mc:Ignorable="d"Title="GridDemo" Height="300" Width="450"><Window.Resources><Style x:Key="Button1Style" TargetType="Button"><Setter Property="Width" Value="100"/><Setter Property="Height" Value="50"/><Setter Property="Background" Value="Blue"/></Style><Style x:Key="Button2Style" TargetType="Button"><Setter Property="Width" Value="100"/><Setter Property="Height" Value="50"/><Setter Property="Background" Value="Red"/></Style></Window.Resources><Grid ShowGridLines="True"><Grid.RowDefinitions><RowDefinition></RowDefinition><RowDefinition></RowDefinition></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition></ColumnDefinition><ColumnDefinition></ColumnDefinition></Grid.ColumnDefinitions><Button Style="{StaticResource Button1Style}" Grid.Row="0" Grid.Column="0">Button 1</Button><Button Style="{StaticResource Button2Style}" Grid.Row="0" Grid.Column="1">Button 2</Button></Grid>
</Window>

这样写,也会有个问题,因为有很多重复的属性样式,看着很鸡肋。

四、继承样式设置属性

在之前的基础上,加上BasedOn特性,可以继承样式。

<Window x:Class="WpfApp1.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:WpfApp1"mc:Ignorable="d"Title="GridDemo" Height="450" Width="600"><Window.Resources><Style x:Key="Button1Style" TargetType="Button"><Setter Property="Width" Value="100"/><Setter Property="Height" Value="50"/><Setter Property="Background" Value="Blue"/></Style><Style x:Key="Button2Style" TargetType="Button" BasedOn="{StaticResource Button1Style }"><Setter Property="Background" Value="Red"/><Setter Property="FontWeight" Value="Bold"/><Setter Property="FontSize" Value="20"/></Style></Window.Resources><Grid ShowGridLines="True"><Grid.RowDefinitions><RowDefinition></RowDefinition><RowDefinition></RowDefinition></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition></ColumnDefinition><ColumnDefinition></ColumnDefinition></Grid.ColumnDefinitions><Button Style="{StaticResource Button1Style}" Grid.Row="0" Grid.Column="0">Button 1</Button><Button Style="{StaticResource Button2Style}" Grid.Row="0" Grid.Column="1">Button 2</Button></Grid>
</Window>

五、使用应用级别的资源

将样式定义在App.xaml文件中,使得整个应用程序中的控件都可以使用这些样式。

//App.xaml 文件
<Application x:Class="WpfApp1.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:WpfApp1"StartupUri="MainWindow.xaml"><Application.Resources><Style x:Key="Button1Style" TargetType="Button"><Setter Property="Width" Value="100"/><Setter Property="Height" Value="50"/><Setter Property="Background" Value="Blue"/></Style><Style x:Key="Button2Style" TargetType="Button" BasedOn="{StaticResource Button1Style }"><Setter Property="Background" Value="Red"/><Setter Property="FontWeight" Value="Bold"/><Setter Property="FontSize" Value="20"/></Style></Application.Resources>
</Application>//MainWindow.xaml 文件
<Application x:Class="WpfApp1.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:WpfApp1"StartupUri="MainWindow.xaml"><Application.Resources><Style x:Key="Button1Style" TargetType="Button"><Setter Property="Width" Value="100"/><Setter Property="Height" Value="50"/><Setter Property="Background" Value="Blue"/></Style><Style x:Key="Button2Style" TargetType="Button" BasedOn="{StaticResource Button1Style }"><Setter Property="Background" Value="Red"/><Setter Property="FontWeight" Value="Bold"/><Setter Property="FontSize" Value="20"/></Style></Application.Resources>
</Application>

六、使用资源字典(Resource Dictionary)

将样式定义在独立的资源字典文件中,然后在需要的地方引用这些字典。有点类似,创建.css文件,在需要的页面引用css文件。

1、VS右键创建资源字典(WPF)文件,命名为CustomerStyle.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><Style x:Key="Button1Style" TargetType="Button"><Setter Property="Width" Value="100"/><Setter Property="Height" Value="50"/><Setter Property="Background" Value="Blue"/></Style><Style x:Key="Button2Style" TargetType="Button" BasedOn="{StaticResource Button1Style }"><Setter Property="Background" Value="Red"/><Setter Property="FontWeight" Value="Bold"/><Setter Property="FontSize" Value="20"/></Style>
</ResourceDictionary>

2、页面中引用CustomerStyle.xaml文件

<Window x:Class="WpfApp1.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:WpfApp1"mc:Ignorable="d"Title="GridDemo" Height="450" Width="600"><Window.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="CustomerStyle.xaml"/></ResourceDictionary.MergedDictionaries></ResourceDictionary></Window.Resources><Grid ShowGridLines="True"><Grid.RowDefinitions><RowDefinition></RowDefinition><RowDefinition></RowDefinition></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition></ColumnDefinition><ColumnDefinition></ColumnDefinition></Grid.ColumnDefinitions><Button Style="{StaticResource Button1Style}" Grid.Row="0" Grid.Column="0">Button 1</Button><Button Style="{StaticResource Button2Style}" Grid.Row="0" Grid.Column="1">Button 2</Button></Grid>
</Window>

<ResourceDictionary Source="xx.xaml"/>,需要注意的是Source后面的文件路径,我是两个页面在同一级目录下。不同目录,路径需要注意。

七、使用触发器(Triggers)

使用Style.Triggers定义条件样式,当条件满足时应用样式。

<Window x:Class="WpfApp.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="MainWindow" Height="350" Width="525"><Window.Resources><Style x:Key="ButtonStyle" TargetType="Button"><Setter Property="Width" Value="100"/><Setter Property="Height" Value="50"/><Setter Property="Background" Value="Blue"/><Style.Triggers><Trigger Property="IsMouseOver" Value="True"><Setter Property="Background" Value="Red"/></Trigger></Style.Triggers></Style></Window.Resources><Grid><Button Style="{StaticResource ButtonStyle}" Content="Hover Me" HorizontalAlignment="Center" VerticalAlignment="Center"/></Grid>
</Window>

当鼠标移动到Button按钮时,会变成红色。(但是这个例子,颜色没有变,可能是我版本的问题。下面是我成功的例子。)

<Window x:Class="WpfApp.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="MainWindow" Height="350" Width="525"><Window.Resources><Style x:Key="ButtonStyle" TargetType="Button"><Setter Property="Width" Value="100"/><Setter Property="Height" Value="50"/><Setter Property="Background" Value="Blue"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="Button"><Border Background="{TemplateBinding Background}"><ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/></Border></ControlTemplate></Setter.Value></Setter><Style.Triggers><Trigger Property="IsMouseOver" Value="True"><Setter Property="Background" Value="Red"/></Trigger></Style.Triggers></Style></Window.Resources><Grid><Button Style="{StaticResource ButtonStyle}" Content="Hover Me" HorizontalAlignment="Center" VerticalAlignment="Center"/></Grid>
</Window>


总结

WPF中提供了多种样式设置方式,可以满足不同的UI设计需求:

  • 内联样式:简单直接,适用于一次性设置。
  • 显式样式:通过资源引用应用样式,适用于多个控件共享样式。
  • 隐式样式:自动应用样式到所有特定类型的控件,简化样式管理。
  • 资源字典:集中管理资源,可以在窗口级别、页面级别或独立文件中定义,便于资源的复用和管理。
  • 应用级别资源:在App.xaml中定义,全局可用,适用于需要在整个应用中共享的资源。

后面用到还会继续补充。

相关文章:

  • 英语阅读文章
  • 探索 Adobe Illustrator 2023 (AI 2023) for Mac/Win——创意设计的强大工具
  • Python_ 爬楼梯
  • Win32和c++11多线程
  • 物联网-高性能时序数据库QuestDB
  • 问题:8255A的端口A工作在方式2时,使用端口C的______作为与CPU和外部设备的联络信号。 #媒体#经验分享#其他
  • 【SpringBoot + Vue 尚庭公寓实战】项目介绍(一)
  • 浅谈提示词发展现状,Prompt 自动优化是未来。
  • 七天进阶elasticsearch[Three]
  • 每日题库:Huawe数通HCIA——全部【813道】
  • 【微信小程序】页面事件
  • SpringCloud 负载均衡 spring-cloud-starter-loadbalancer
  • Hadoop3:MapReduce源码解读之Map阶段的CombineFileInputFormat切片机制(4)
  • vllm 大模型量化微调推理使用: lora、gptq、awq
  • 探索Linux中的gzip命令:压缩与解压缩的艺术
  • const let
  • CoolViewPager:即刻刷新,自定义边缘效果颜色,双向自动循环,内置垂直切换效果,想要的都在这里...
  • Java反射-动态类加载和重新加载
  • js算法-归并排序(merge_sort)
  • KMP算法及优化
  • PHP的类修饰符与访问修饰符
  • Python 反序列化安全问题(二)
  • React 快速上手 - 07 前端路由 react-router
  • React-生命周期杂记
  • SpiderData 2019年2月16日 DApp数据排行榜
  • Twitter赢在开放,三年创造奇迹
  • 短视频宝贝=慢?阿里巴巴工程师这样秒开短视频
  • 干货 | 以太坊Mist负责人教你建立无服务器应用
  • 给自己的博客网站加上酷炫的初音未来音乐游戏?
  • 关于extract.autodesk.io的一些说明
  • 算法-插入排序
  • 无服务器化是企业 IT 架构的未来吗?
  • 小程序开发中的那些坑
  • 7行Python代码的人脸识别
  • (ctrl.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MDd_DynamicDebug”不匹配值“
  • (Git) gitignore基础使用
  • (Oracle)SQL优化技巧(一):分页查询
  • (Redis使用系列) Springboot 使用Redis+Session实现Session共享 ,简单的单点登录 五
  • (附源码)计算机毕业设计SSM疫情居家隔离服务系统
  • (附源码)计算机毕业设计高校学生选课系统
  • (一)WLAN定义和基本架构转
  • (转)拼包函数及网络封包的异常处理(含代码)
  • (转)视频码率,帧率和分辨率的联系与区别
  • .h头文件 .lib动态链接库文件 .dll 动态链接库
  • .NET CORE 3.1 集成JWT鉴权和授权2
  • .NET开发不可不知、不可不用的辅助类(一)
  • .NET连接MongoDB数据库实例教程
  • .NET企业级应用架构设计系列之开场白
  • @entity 不限字节长度的类型_一文读懂Redis常见对象类型的底层数据结构
  • [ 云计算 | AWS 实践 ] Java 如何重命名 Amazon S3 中的文件和文件夹
  • [AI aider] 打造终端AI搭档:Aider让编程更智能更有趣!
  • [BZOJ5125]小Q的书架(决策单调性+分治DP+树状数组)
  • [C#]使用C#部署yolov8-seg的实例分割的tensorrt模型
  • [C++] Windows中字符串函数的种类
  • [CF703D]Mishka and Interesting sum/[BZOJ5476]位运算