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. 关于我第二周学习kotlin这门语言

    有关kotlin的知识点: 在学习lambda之前,我们先了解一下什么是lambda,简答来说就是一小段代码块,并且我们可以将这个代码块在函数之间传递,这是函数式编程的一个重要特性. 通常我们会需要一 ...

  2. DBeaver连接mysql时Public Key Retrieval is not allowed错误

    前言 DBeaver 连接 mysql 时,报错:Public Key Retrieval is not allowed 解决 在新建连接的时候,驱动属性里设置 allowPublicKeyRetri ...

  3. CAD通过XCLIP命令插入DWG参照裁剪图形,引用局部图像效果(CAD裁剪任意区域)

    CAD通过XCLIP命令插入DWG参照裁剪图形,实现引用局部图像效果,裁剪任意区域! 1.首先在你要引用局部图的文件内,插入参照! 2. 然后再空白区域指定插入点,输入比例因子,默认输入1,然后缩小视 ...

  4. js调用本地程序资源-兼容所有浏览器

    在网页上通过JavaScript调用本地程序,兼容IE8/9/10/11.Opera.Chrome.Safari.Firefox等所有浏览器,在做Web开发时经常会遇到需要调用本地的一些exe或者dl ...

  5. 实现领域驱动设计 - 使用ABP框架 - 系列文章汇总

    系列文章汇总 前言: 最近看到ABP官网的一本电子书,感觉写的很好,翻译出来,一起学习下 Implementing Domain Driven Design 实现领域驱动设计 - 使用ABP框架 - ...

  6. 【SpringMVC】运行流程

    SpringMVC 运行流程 在 Spring 的环境下使用 SpringMVC Bean 被创建两次? Spring 的 IOC 容器不应该扫描 SpringMVC 中的 bean, 对应的 Spr ...

  7. DevOps系列——Jenkins私服

    DevOps基础设施较多,所以客官不要太着急,要有个"渐进明细"的过程,前面说了GitLab,这里再说下Jenkins,这俩算 是较为核心的基础组件,其他组件可选项较多,而这俩的地 ...

  8. SQL 和 PL/SQL 的区别

    不经意看到2个ORA错误,一个提示PL/SQL ORA-错误,另一个提示SQL ORA-错误,好奇这2货啥区别?留爪. PL/SQL也是一种程序语言,叫做过程化SQL语言(Procedural Lan ...

  9. 学习unigui【23】uniDBGrid的使用摘要

    Unidbgrid自动调整列宽 UniDBGrid1 -> ClientEvents -> ExtEvents [Ext.data.Store[store] ] add store.loa ...

  10. [开源] 分享一个自己开发的, 整合SMS/Mail/Telegram/微信四个平台的开源信息收发平台

    起因于已有的聚合信息发送平台无法满足自己的需求. 不支持我需要的平台,或不支持接收信息后进行处理,或不放心把涉及隐私的消息通过第三方平台发送 利用SMS发送短信(上一篇文章中分享的开源项目) 利用SM ...