Style:用法,多样性,全局样式与资源字典
Style:用法,多样性,全局样式与资源字典
对应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:用法,多样性,全局样式与资源字典的更多相关文章
- BootStrap入门教程 (一) :手脚架Scaffolding(全局样式(Global Style),格网系统(Grid System),流式格网(Fluid grid System),自定义(Customing),布局(Layouts))
2011年,twitter的“一小撮”工程师为了提高他们内部的分析和管理能力,用业余时间为他们的产品构建了一套易用.优雅.灵活.可扩展的前端工具集--BootStrap.Bootstrap由MARK ...
- WPF学习笔记-使用自定义资源字典(style)文件
1.添加资源字典文件style.xmal 2.在资源字典中添加自定义style等 <ResourceDictionary xmlns="http://schemas.microsoft ...
- C#获取起始位置以及添加全局资源字典
获取起始位置 Path.Combine(AppDomain.CurrentDomain.BaseDirectory); 添加全局资源 string temp = "this is a str ...
- 全局css , 样式设置, css 初始化. css ,style ,全局样式, 初始化样式
全局CSS设置总结 1.清除所有标记的内外边距 html, body, ul, li, ol, dl, dd, dt, p, h1, h2, h3, h4, h5, h6, form, fieldse ...
- Windows Phone 三、样式和资源
定义样式和引用资源 <Page.Resources> <!-- 向资源字典中添加一个键为ButtonBackground值为SolidColorBrush对象 --> < ...
- WPF资源字典使用
资源字典出现的初衷就在于可以实现多个项目之间的共享资源,资源字典只是一个简单的XAML文档,该文档除了存储希望使用的资源之外,不做任何其它的事情. 1. 创建资源字典 创建资源字典的过程比较简单,只 ...
- Silverlight实用窍门系列:68.Silverlight的资源字典ResourceDictionary
允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://chengxingliang.blog.51cto.com/3972944/886643 ...
- WPF 为资源字典 添加事件响应的后台类
原文:WPF 为资源字典 添加事件响应的后台类 前言,有许多同学在写WPF程序时在资源字典里加入了其它控件,但又想写事件来控制这个控件,但是资源字典没有CS文件,不像窗体XAML还有一个后台的CS文件 ...
- WPF——如何为项目设置全局样式。
在项目中,需要为所有的Button.TextBox设置一个默认的全局样式,一个个的为多个控件设置相同的样式显然是不明智的.在WPF中可以通过资源设置全局样式,主要有俩种方法: 1.第一种就是先写好按钮 ...
- WPF 精修篇 管理资源字典
原文:WPF 精修篇 管理资源字典 样式太多 每个界面可能需要全局的样式 有没有肯能 WPF 中的样式 像Asp.net中 的CSS一样管理那 有的 有资源字典 BurshDictionary &l ...
随机推荐
- nginx启动失败 Starting nginx nginx [fail]
nginx -t :测试配置文件是否有语法错误 看看报什么错误,我的是忘记给权限了 nginx: [alert] could not open error log file: open() " ...
- 神经网络与模式识别课程报告-卷积神经网络(CNN)算法的应用
======================================================================================= 完整的神经网络与模式识别 ...
- js调用本地程序资源-兼容所有浏览器
在网页上通过JavaScript调用本地程序,兼容IE8/9/10/11.Opera.Chrome.Safari.Firefox等所有浏览器,在做Web开发时经常会遇到需要调用本地的一些exe或者dl ...
- JAVA调用Python脚本执行
SpringBoot-web环境 <dependency> <groupId>org.springframework.boot</groupId> <arti ...
- 一文速通Python并行计算:04 Python多线程编程-多线程同步(上)—基于条件变量、事件和屏障
一文速通 Python 并行计算:04 Python 多线程编程-多线程同步(下)-基于条件变量.事件和屏障 摘要: 本文介绍了 Python 多线程同步的三种机制:条件变量(Condition).事 ...
- langchain0.3教程:从0到1打造一个智能聊天机器人
在上一篇文章<大模型开发之langchain0.3(一):入门篇> 中已经介绍了langchain开发框架的搭建,最后使用langchain实现了HelloWorld的代码案例,本篇文章将 ...
- leetcode每日一题:向字符串添加空格
题目 2109. 向字符串添加空格 给你一个下标从 0 开始的字符串 s ,以及一个下标从 0 开始的整数数组 spaces . 数组 spaces 描述原字符串中需要添加空格的下标.每个空格都应该插 ...
- [COCI2014-2015#2] MOBITEL 题解
题目大意 有一只蚂蚱,它把手机掉到了水坑里.然后它把手机捞出来,发现手机键盘都坏了. 那么手机没有坏之前就是介个样子的: 我们想打字的话就需要按下相应的数字键.比如说我们想打出 "a&quo ...
- 一文彻底搞懂 MCP:AI 大模型的标准化工具箱
MCP 最近在 AI 领域 引发了 广泛关注,特别是在 海外各大社区 中,大家热烈讨论,热度 相当高. 我打开了 Google Trends,这是一个专门用于查看全球热点趋势的网站. 输入关键词后,可 ...
- 测试工作中用到的MongoDB命令
1.远程连接MongoDB mongo host:port/dbname (host和port根据自己需要修改) 连接成功页面如下: 2.显示所有数据库 show dbs 3.切换到oversea-a ...