Style:用法,多样性,全局样式与资源字典

本文同时为b站WPF课程的笔记,相关示例代码

对应06~08

前言

大部分能够想到的属性,xaml里面都是自带了的。可以多去网络上搜一搜。比如说高度、宽度、文本、颜色、背景色。

通常是这样控制属性的:

<Button Grid.Column="1" Width="100" Height="100" Content="我是两倍" Background="AliceBlue"/>

这样就控制了它所在的位置,按钮的高度、宽度、文本和背景颜色等等。

还可以在标签之间加入属性。例如:

<Button Grid.Column="1" Width="100" Height="100" Background="AliceBlue">
<Button.Content>我是两倍</Button.Content>
</Button>

甚至这样:

<Button Grid.Column="1">
<Button.Content>我也是一倍</Button.Content>
<Button.Width>100</Button.Width>
<Button.Height>100</Button.Height>
<Button.Background>AliceBlue</Button.Background>
</Button>

和刚刚是一样的效果。

但是假如我们需要给多个按钮实现相同的尺寸大小背景色,那么每一个按钮都需要附加上这么多个属性,而且也不好统一调整。

所以我们引入 Style 样式。

Style 用法

<Window>标签内开始定义:

<Window.Resources>
<Style TargetType="控件">
<Setter Property="控件属性" Value="控件属性的值"/>
<Setter Property="控件属性" Value="控件属性的值"/>
<Setter Property="Width" Value="500"/>
<Setter Property="FontSize" Value="15"/>
</Style>
</Window.Resources>

修改默认样式

<Window.Resources>
<Style TargetType="Button">
<Setter Property="Background" Value="Red"/>
<Setter Property="Height" Value="50"/>
<Setter Property="Width" Value="500"/>
<Setter Property="FontSize" Value="15"/>
</Style>
</Window.Resources>

如果之后定义的任何 Button ,没有定义背景色、高度、宽度、字体大小属性的话,就会默认使用这里预先定义好的。如果之后定义了,则会覆盖。

多样式

通常我们并不会给所有的按钮只用上一种样式。比如一个登录的界面,应该会有登录、退出、注销、忘记密码等按钮。每一个或者多个对应一种样式。

当样式定义中包含key属性时,该样式不会被自动应用,需要显式地在控件中引用。

通过给 style 包含key属性,定义多组样式:

<Window.Resources>
<Style x:Key="LoginStyle" TargetType="Button">
<Setter Property="Background" Value="Blue"/>
<Setter Property="Height" Value="50"/>
<Setter Property="Width" Value="500"/>
<Setter Property="FontSize" Value="15"/>
</Style>
<Style x:Key="QuitStyle" TargetType="Button">
<Setter Property="Background" Value="Red"/>
<Setter Property="Height" Value="50"/>
<Setter Property="Width" Value="500"/>
<Setter Property="FontSize" Value="15"/>
</Style>
<Style x:Key="Forgetstyle" TargetType="Button">
<Setter Property="Background" Value="Yellow"/>
<Setter Property="Height" Value="50"/>
<Setter Property="Width" Value="500"/>
<Setter Property="FontSize" Value="15"/>
</Style>
</Window.Resources>

这里定义了三种样式,分别应用于登录按钮、退出按钮和忘记密码按钮。

应用样式的时候如下

<Button Style="{StaticResource LoginStyle}" Content="登录"/>
<Button Style="{StaticResource QuitStyle}" Content="退出"/>
<Button Style="{StaticResource Forgetstyle}" Content="忘记密码"/>

其中StaticResource表示这是一个静态资源。

对应的也有动态资源,后续再说。

默认+差异化

结合两种方式,我们修改默认样式来设置一些基础属性,同时再生成几个具有差异化的样式,分别应用。

<Window.Resources>
<Style TargetType="Button">
<Setter Property="Height" Value="50"/>
<Setter Property="Width" Value="200"/>
<Setter Property="FontSize" Value="15"/>
<Setter Property="Margin" Value="10"/>
</Style>
<Style x:Key="LoginStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="Blue"/>
</Style>
<Style x:Key="QuitStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="Red"/>
</Style>
<Style x:Key="Forgetstyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="Yellow"/>
</Style>
</Window.Resources>

这段代码描述了4个样式,第一个样式是定义了Button的默认样式,接下来3个样式继承了默认样式,同时又修改了背景颜色。

BasedOn="{StaticResource {x:Type Button}}"是指继承自静态资源中的类型Button的样式

全局样式

在当前Window下定义的样式只会作用于当前的Window

为了让某种样式可以在整个页面/项目中通用,我们需要将样式挪到外部。

右键项目,添加资源字典。这里就叫做BaseButtomStyle.xaml

首先可以将上面在 Window 中定义的样式直接剪切ResourceDictionary中。不要复制 <Window.Resources>了,因为现在是给全局的样式下定义,而非仅对于Window中的按钮。即:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Button">
<Setter Property="Height" Value="50"/>
<Setter Property="Width" Value="200"/>
<Setter Property="FontSize" Value="15"/>
<Setter Property="Margin" Value="10"/>
</Style>
<Style x:Key="LoginStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="Blue"/>
</Style>
<Style x:Key="QuitStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="Red"/>
</Style>
<Style x:Key="Forgetstyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="Yellow"/>
</Style>
</ResourceDictionary>

接下来还需要在app.xaml中用上BaseButtomStyle.xaml

打开app.xaml,在<Application.Resources>标签中写入

<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/你的项目名称;component/资源文件的相对路径"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

浅浅勘误一下。老师在第八节课的第8分钟:项目名称component 之间不是冒号:,而是分号;

例如,我的项目名称是WPF_Study,资源文件相对路径为BaseButtomStyle.xaml(即,我没有把资源文件放在任何一个子文件夹中),那么完整的App.xaml如下:

<Application x:Class="WPF_Study.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF_Study"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/WPF_Study;component/BaseButtomStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>

注意,这里输入的是相对路径。如果这里的资源文件BaseButtomStyle.xaml是放在某个子文件夹下的,那么就应该改成<ResourceDictionary Source="/WPF_Study;component/子文件夹/BaseButtomStyle.xaml"/>

至此,在任何界面中都可以访问到BaseButtomStyle.xaml中定义的样式。访问方法与之前的局部样式无异

总结

定义局部样式

在需要的窗口的<Window>标签之后添加如下样式定义。

不定义x:Key,则为默认属性。

定义了x:Key,则需要显式地在控件中引用。

<Window.Resources>
<Style TargetType="Button">
<Setter Property="Height" Value="50"/>
<Setter Property="Width" Value="200"/>
<Setter Property="FontSize" Value="15"/>
<Setter Property="Margin" Value="10"/>
</Style>
<Style x:Key="LoginStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="Blue"/>
</Style>
<Style x:Key="QuitStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="Red"/>
</Style>
<Style x:Key="Forgetstyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="Yellow"/>
</Style>
</Window.Resources>

定义全局样式

在合适的地方建立资源字典文件:右键项目,添加,资源字典。

添加自定义样式:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Button">
<Setter Property="Height" Value="50"/>
</Style>
<Style x:Key="LoginStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="Blue"/>
</Style>
</ResourceDictionary>

App.xaml中引用上资源文件:

<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/项目名称;component/资源词典文件相对路径"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>

使用样式

不定义x:Key的样式会自动应用

若定义了,则修改对应控件的属性Style="{StaticResource 样式名称}"以应用样式

局部定义的样式与全局定义的样式在使用上没有区别。

Style:用法,多样性,全局样式与资源字典的更多相关文章

  1. BootStrap入门教程 (一) :手脚架Scaffolding(全局样式(Global Style),格网系统(Grid System),流式格网(Fluid grid System),自定义(Customing),布局(Layouts))

    2011年,twitter的“一小撮”工程师为了提高他们内部的分析和管理能力,用业余时间为他们的产品构建了一套易用.优雅.灵活.可扩展的前端工具集--BootStrap.Bootstrap由MARK ...

  2. WPF学习笔记-使用自定义资源字典(style)文件

    1.添加资源字典文件style.xmal 2.在资源字典中添加自定义style等 <ResourceDictionary xmlns="http://schemas.microsoft ...

  3. C#获取起始位置以及添加全局资源字典

    获取起始位置 Path.Combine(AppDomain.CurrentDomain.BaseDirectory); 添加全局资源 string temp = "this is a str ...

  4. 全局css , 样式设置, css 初始化. css ,style ,全局样式, 初始化样式

    全局CSS设置总结 1.清除所有标记的内外边距 html, body, ul, li, ol, dl, dd, dt, p, h1, h2, h3, h4, h5, h6, form, fieldse ...

  5. Windows Phone 三、样式和资源

    定义样式和引用资源 <Page.Resources> <!-- 向资源字典中添加一个键为ButtonBackground值为SolidColorBrush对象 --> < ...

  6. WPF资源字典使用

    资源字典出现的初衷就在于可以实现多个项目之间的共享资源,资源字典只是一个简单的XAML文档,该文档除了存储希望使用的资源之外,不做任何其它的事情. 1.  创建资源字典 创建资源字典的过程比较简单,只 ...

  7. Silverlight实用窍门系列:68.Silverlight的资源字典ResourceDictionary

      允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://chengxingliang.blog.51cto.com/3972944/886643 ...

  8. WPF 为资源字典 添加事件响应的后台类

    原文:WPF 为资源字典 添加事件响应的后台类 前言,有许多同学在写WPF程序时在资源字典里加入了其它控件,但又想写事件来控制这个控件,但是资源字典没有CS文件,不像窗体XAML还有一个后台的CS文件 ...

  9. WPF——如何为项目设置全局样式。

    在项目中,需要为所有的Button.TextBox设置一个默认的全局样式,一个个的为多个控件设置相同的样式显然是不明智的.在WPF中可以通过资源设置全局样式,主要有俩种方法: 1.第一种就是先写好按钮 ...

  10. WPF 精修篇 管理资源字典

    原文:WPF 精修篇 管理资源字典 样式太多  每个界面可能需要全局的样式 有没有肯能 WPF 中的样式 像Asp.net中 的CSS一样管理那 有的 有资源字典 BurshDictionary &l ...

随机推荐

  1. 使用Python完成设备巡检

    在企业网络中,设备巡检是保持网络稳定性和安全性的核心任务.无论是路由器.交换机,还是防火墙和服务器等设备,都需要定期进行巡检,以确保网络设施的正常运行.然而,传统的设备巡检通常是通过手动登录设备.查看 ...

  2. 【HTML】步骤进度条组件

    HTML步骤进度条 效果图 思路 分份: 有多少个步骤就可以分成多少分,每份宽度应该为100%除以步骤数,故以上效果图中的每份宽度应该为25%,每份用一个div. 每份: 每份中可以看成是三个元素,一 ...

  3. cnpack导致view快捷键失灵。

    学习d10.3.出现怪问题: 卸载cnpack出现: 这下要用快捷键了.那可不烦透了. 如此就ok了. 鸡蛋好吃,还要知道母鸡如何生蛋的?

  4. D常用快捷键大全(转)

    Ctrl+PageUp将光标移至本屏的第一行,屏幕不滚动.Ctrl+PageDown将光标移至本屏的最后一行,屏幕不滚动.Ctrl+↓向下滚动屏幕,光标跟随滚动不出本屏.Ctrl+↑向上滚动屏幕,光标 ...

  5. chatops

    ChatOps是什么? ChatOps, 简单地说,这是一种方法,允许团队以聊天室的方式来协作和管理其基础结构.代码和数据的许多方面.通过使用聊天机器人和脚本,团队可以执行命令.查询信息,并将知识分发 ...

  6. mybatis报错Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.ArrayList and java

    原因 传入参数为List<String>不能用lists != '' 判断 解决 将lists != '' 的判断去掉或者改为lists .size>0 其他 如果是Integer类 ...

  7. 使用注解的方式编写:@Aspect运用

    列子. public interface Calculator { // 加 public int add(int i,int j); // 减 public int sub(int i,int j) ...

  8. Asp.net mvc基础(七)cshtml页面中自动using

    场景如下: 在cshtml页面中,如果要在页面中进行初始化一个类的时候,需要添加这个类的命名空间才可以. 对于这种情况,我们可以通过配置Web.config达到在cshtm页面的使用类的时候不需要进行 ...

  9. C#/.NET/.NET Core技术前沿周刊 | 第 34 期(2025年4.7-4.13)

    前言 C#/.NET/.NET Core技术前沿周刊,你的每周技术指南针!记录.追踪C#/.NET/.NET Core领域.生态的每周最新.最实用.最有价值的技术文章.社区动态.优质项目和学习资源等. ...

  10. FastAPI与Tortoise-ORM实现关系型数据库关联

    title: FastAPI与Tortoise-ORM实现关系型数据库关联 date: 2025/04/21 10:51:41 updated: 2025/04/21 10:51:41 author: ...