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. QWidget的isHidden和isVisible

    文章目录 QWidget的isHidden和isVisible 问题的出现 QWidget的show()函数 QWidget的isVisible和isHidden 源码追溯 QWidget的isHid ...

  2. 【前端动画】—— 再看tweenJS

    16开始接触前端,一直对一个问题特别感兴趣,那就是js动画,也就是从那时起开始探究动画的各种表现形式,也是那个时候开始意识到编程这块东西最终考验的就是抽象和逻辑,而这一切完全是数学里边的东西. 最早接 ...

  3. windows nvm 切换node版本后,npm找不到

    前言 在 windows 使用 nvm,管理 node 版本时,nvm install 14.21.3 后,发现在指定 node 版本的 node_modules 文件夹中没有对应的 npm 包,这时 ...

  4. Golang入门:Linux上的go语言安装与配置

    Tips:本文以本文撰写时的 Go 语言最新版本,也就是 go.1.19.2 版本为例. Linux 发行版本使用 Ubuntu 22.04.1 LTS 为例来做演示. 安装 C 工具 Go 的工具链 ...

  5. 容器到底是个啥?(附Docker学习资源汇总)

    目录Docker与容器    初识容器与Docker    为什么要使用Docker    Docker优势简介Docker核心概念    Docker客户端和服务器    Docker镜像    D ...

  6. 深入理解泛型-重写泛型类方法遇到的问题(涉及JVM反编译字节码)

    下面的代码DateInterval类想重写父类Pair<LocalDate>中的setSecond方法,保证设置的第二个日期要在第一个日期之后,不能出现second早于first的情况.这 ...

  7. MySQL-脏页的刷新机制

    MySQL内存结构-缓冲区 MySQL的缓冲区中有数据页,索引页,插入缓冲等等,这个角度是从页的功能来分类的.本小节从另一个视角关注这些页,如果从 是否被修改过(和磁盘不一致) 这个角度来区分这些页, ...

  8. 【Python】pip安装加速:使用国内镜像源

    [Python]pip安装加速:使用国内镜像源 零.使用命令行设置 一.设置全局镜像源 随便使用下面任一命令即可 阿里云: pip config set global.index-url https: ...

  9. freertos消息队列的值传递和指针传递

    消息队列的使用方法总结: 1.消息队列初始化(定义一个消息队列的结构体),一般在main.c中完成. 2.消息队列的发送:   a extern 消息队列   b 定义一个结构体的指针指向消息消息队列 ...

  10. Python科学计算系列1—方程和方程组

    1.一元方程求解 例1:求下列一元二次方程的解 代码如下: # 定义数学符号 from sympy import symbols, solve x = symbols('x') f = x ** 2 ...