WPF2 样式布局

样式布局

WPF中的各类控件元素, 都可以自由的设置其样式。 诸如:

字体(FontFamily)
字体大小(FontSize)
背景颜色(Background)
字体颜色(Foreground)
边距(Margin)
水平位置(HorizontalAlignment)
垂直位置(VerticalAlignment) 等等。

而样式则是组织和重用以上的重要工具。不是使用重复的标记填充XAML, 通过Styles创建一系列封装所有这些细节的样式。然后通过Style属性应用封装好的样式。这点类似于CSS样式。然而, WPF样式的功能更加强大, 如控件的行为。WPF的样式还支持触发器(后面章节会讲到)。

示例

<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="MainWindow" Height="450" Width="800"><Grid><StackPanel><Button Content="button1" FontSize="18" Foreground="White" Background="Red"/><Button Content="button1" FontSize="18" Foreground="White" Background="Red"/><Button Content="button1" FontSize="18" Foreground="White" Background="Red"/><Button Content="button1" FontSize="18" Foreground="White" Background="Red"/></StackPanel></Grid>
</Window>

在这里插入图片描述

优化

样式是组织和重用的工具。 而上面的代码, 由于每个元素都是相同的, 但是每个元素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="MainWindow" Height="450" Width="800"><Window.Resources><Style TargetType="Button"><Setter Property="FontSize" Value="18"/><Setter Property="Foreground" Value="White"/><Setter Property="Background" Value="Red"/><Setter Property="Content" Value="button1"/></Style></Window.Resources><Grid><StackPanel><Button /><Button /><Button /><Button /></StackPanel></Grid>
</Window>

此时按钮的样式还在
在这里插入图片描述
单独定义
每个控件的外观

<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="MainWindow" Height="450" Width="800"><Window.Resources><Style x:Key="ButtonStyle" TargetType="Button"><Setter Property="FontSize" Value="18"/><Setter Property="Foreground" Value="White"/><Setter Property="Background" Value="Red"/><Setter Property="Content" Value="button1"/></Style></Window.Resources><Grid><StackPanel><Button Style="{StaticResource ButtonStyle}"/><Button Style="{StaticResource ButtonStyle}"/><Button /><Button /></StackPanel></Grid>
</Window>

在这里插入图片描述
BaseOn 继承样式
基类往往定义公共元素

<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="MainWindow" Height="450" Width="800"><Window.Resources><Style x:Key="BaseButtonStyle" TargetType="Button"><Setter Property="FontSize" Value="18"/><Setter Property="Foreground" Value="White"/><Setter Property="Background" Value="Red"/></Style><Style x:Key="ButtonStyle" TargetType="Button" BasedOn="{StaticResource BaseButtonStyle}"><Setter Property="Content" Value="button1"/></Style></Window.Resources><Grid><StackPanel><Button Style="{StaticResource ButtonStyle}"/><Button Style="{StaticResource ButtonStyle}"/><Button /><Button /></StackPanel></Grid>
</Window>

样式完全相同
在这里插入图片描述
Style和 控件内同时有相同元素时候,元素内优先级永远是最高的。会覆盖样式中的同类属性

<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="MainWindow" Height="450" Width="800"><Window.Resources><Style x:Key="BaseButtonStyle" TargetType="Button"><Setter Property="FontSize" Value="18"/><Setter Property="Foreground" Value="White"/><Setter Property="Background" Value="Red"/></Style><Style x:Key="ButtonStyle" TargetType="Button" BasedOn="{StaticResource BaseButtonStyle}"><Setter Property="Content" Value="button1"/></Style></Window.Resources><Grid><StackPanel><Button Content="hello1" Style="{StaticResource ButtonStyle}"/><Button Content="hello2" Style="{StaticResource ButtonStyle}"/><Button /><Button /></StackPanel></Grid>
</Window>

这里改变了控件内Content内容

在这里插入图片描述

TargetType 的设置为类型必须和控件保持一致,否者不可用

<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="MainWindow" Height="450" Width="800"><Window.Resources><!--这里改变为checkbook--><Style x:Key="BaseButtonStyle" TargetType="CheckBox"><Setter Property="FontSize" Value="18"/><Setter Property="Foreground" Value="White"/><Setter Property="Background" Value="Red"/></Style><Style x:Key="ButtonStyle" TargetType="Button" BasedOn="{StaticResource BaseButtonStyle}"><Setter Property="Content" Value="button1"/></Style></Window.Resources><Grid><StackPanel><Button Content="hello1" Style="{StaticResource ButtonStyle}"/><Button Content="hello2" Style="{StaticResource ButtonStyle}"/><Button /><Button /></StackPanel></Grid>
</Window>

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/1983.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Docker基础+虚拟化概念

目录 一、虚拟化简介 1、虚拟化概述 2、cpu的时间分片&#xff08;cpu虚拟化&#xff09; 3、cpu虚拟化性性能瓶颈 4、虚拟化工作 4.1虚拟机工作原理 4.2两大核心组件:QEMU、KVM 4.2.1QEMU&#xff1a; 4.2.2KVM&#xff1a; 5、虚拟化类型 ①全虚拟化&#xff1a; …

MySQL-数据目录

一、MySQL的主要目录结构&#xff08;MySQL 8&#xff09; [rootlocalhost ~]# find / -name mysql find: ‘/proc/30845’: 没有那个文件或目录 find: ‘/proc/30855’: 没有那个文件或目录 /etc/logrotate.d/mysql /etc/selinux/targeted/active/modules/100/mysql /etc/sel…

国内开通chatgpt plus会员方法

ChatGPT镜像 今天在知乎看到一个问题&#xff1a;“平民不参与内测的话没有账号还有机会使用ChatGPT吗&#xff1f;” 从去年GPT大火到现在&#xff0c;关于GPT的消息铺天盖地&#xff0c;真要有心想要去用&#xff0c;途径很多&#xff0c;别的不说&#xff0c;国内GPT的镜像…

微软如何打造数字零售力航母系列科普02 --- 微软低代码应用平台加速企业创新 - 解放企业数字零售力

微软低代码应用平台推动企业创新- 解放企业数字零售力 微软在2023年GARTNER发布的魔力象限图中处于头部领先&#xff08;leader&#xff09;地位。 其LCAP产品是Microsoft Power Apps&#xff0c;扩展了AI Builder、Dataverse、Power Automate和Power Pages&#xff0c;这些都包…

【26考研】考研备考计划4.22开始

A海海: 408:重中之重&#xff0c;和数学同等地位&#xff01;越早开始越好&#xff01;前期直接跟着王道视频课学习&#xff0c;教材直接用王道四本书&#xff0c;顺序结构的话按照数据结构-计算机组成原理-操作系统-计算机网络的顺序来学习。刚开始学会感觉很吃力很难&#xf…

AutoCodeRover: Autonomous Program Improvement

AutoCodeRover&#xff1a;自主程序改进 Abstract 过去几十年来&#xff0c;研究人员在软件开发过程自动化方面取得了重大进展。大型语言模型 (LLM) 的最新进展对开发过程产生了重大影响&#xff0c;开发人员可以使用基于 LLM 的编程助手来实现自动化编码。然而&#xff0c;软…

【Ne4j图数据库入门笔记2】数据导入详解

2.1 导入 CSV 文件 Cypher中 LOAD CSV 的命令允许我们指定文件路径、标头与否、不同的值分隔符以及 Cypher 语句&#xff0c;用于我们如何在图形中对表格数据进行建模。 CSV 是逗号分隔值的文件&#xff0c;通常在 Excel 或其他电子表格工具中查看。可以有其他类型的值作为分…

spring.factories中配置ApplicationContextInitializer实现类却不起作用

自定义了一个ApplicationContextInitializer的实现类如下 public class MyApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {Overridepublic void initialize(ConfigurableApplicationContext applicationCon…

Vue3 Vite配置环境变量

Vue3 Vite配置环境变量 相关文档配置.env文件vite.config.jspackage.json 使用 相关文档 Vite 官方中文文档&#xff1a;https://cn.vitejs.dev/环境变量和模式&#xff1a;https://cn.vitejs.dev/guide/env-and-mode.html#env-file在配置中使用环境变量&#xff1a;https://c…

SCADA系统通过巨控GRM模块实现OPC协议远程监控PLC

SCADA系统和PLC不在同一个地方&#xff0c;需要远程监控和控制PLC&#xff0c;可以通过巨控GRM模块来实现&#xff0c;通过OPC协议转巨控服务器远程读写PLC寄存器&#xff0c;从而完成远程监控PLC。 要实现SCAKDA系统远程监控PLC&#xff0c;关键是要实现SKADA能通过互联网访问…

vue3+leaflet开发地图入门教程(超级详细)

vue3leaflet开发地图01 1.离线地图下载 ​ 离线地图下载器有很多&#xff0c;网络上也很多文档&#xff0c;这里不再详细说明&#xff0c;根据项目要求下载对应的瓦片地图就好 2.leaflet官网及地图加载 ​ Leaflet - 一个交互式地图 JavaScript 库 (leafletjs.cn) ​ 官网…

Java动态代理与Spring AOP中的Cglib动态代理详解

在Java编程中&#xff0c;动态代理是一种在运行时动态创建代理类及其对象的技术。通过动态代理&#xff0c;我们可以在不修改原有类代码的情况下&#xff0c;为这些类添加新的行为或功能。Java提供了两种主要的动态代理机制&#xff1a;基于接口的Java动态代理和基于类的Cglib动…

密钥派生算法介绍 及 PBKDF2(过时)<Bcrypt(开始淘汰)<Scrypt< Argon2(含Argon2d、Argon2i、Argon2id)简介

密钥派生算法介绍 https://blog.csdn.net/xcxhzjl/article/details/127297263 一、定义 密钥派生函数(Key Derivation Function)就是从一个密码产生出一个或多个密钥&#xff0c;具体就是从一个master key&#xff0c;password或者passphrase派生出一个或多个密钥&#xff0…

HCIP-Datacom-ARST必选题库_23_SNMP【1道题】

一、单选 1.某中型规模园区网络通过SNMP协议管理网络,该园区对于网络安SNMP哪个版本进行管理? 所有版本均可以实现 BSNMPV1 SNMPV2C SNMPV3

都2024 年了,可以卸载的VS Code 插件

在 VS Code 中&#xff0c;庞大的插件市场提供了丰富多样的扩展功能&#xff0c;以增强编码体验和效率。然而&#xff0c;如果你安装了很多插件&#xff0c;就可能会导致&#xff1a; 性能下降&#xff1a;过多的插件可能导致 VS Code 的启动速度变慢&#xff0c;特别是在启动或…

[2021最新]大数据平台CDH存储组件kudu之启用HA高可用(添加多个master)

今天在做kudu高可用的时候没有参考官网&#xff0c;直接按照常规方式&#xff08;添加角色—>编辑属性—>启动&#xff09;结果发现报错&#xff1f;然后参考了一下文档之后发现这玩意儿还有点玄学&#xff0c;做一下记录。 1.添加两个master。kudu master有leader和foll…

薪酬调整流程:规范流程并确保公平合理

薪酬调整是企业人力资源管理中不可或缺的一环&#xff0c;它直接关系到员工的切身利益和企业的发展。为了确保薪酬调整的公平性和合理性&#xff0c;制定一套规范的薪酬调整流程至关重要。本文将详细阐述薪酬调整流程的各个环节&#xff0c;以及如何确保流程的合理性和公平性。…

用云手机运营TikTok有什么好处?

在数字化浪潮的推动下&#xff0c;社交媒体平台正重塑商业推广与品牌建设的面貌。TikTok&#xff0c;这款全球热门的短视频应用&#xff0c;已经吸引了亿万用户的瞩目。对于出海电商和品牌推广而言&#xff0c;借助云手机运营TikTok&#xff0c;能够解锁更多潜在可能&#xff0…

【Linux开发 第十二篇】搭建JavaEE环境

搭建开发环境 搭建javaEE环境 搭建javaEE环境 在Linux下开发JavaEE需要安装软件包&#xff1a; 安装jdk 安装步骤&#xff1a; 在opt目录下创建jdk目录通过xftp上床到jdk目录中进入到jdk目录中&#xff0c;解压jdk压缩包在/usr/local下创建java目录将解压完成的jdk文件移动…

时间搜索

时间搜索 vue2antd <a-form-item label"订单开始时间:"><a-range-picker v-model"date" valueFormat"YYYY-MM-DD" change"onChange" /></a-form-item> onChange (date) {if (date.length > 0) {this.queryParam.…