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.mfbz.cn/a/575013.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

解码Linux中的Shell:一探脚本起源、发展与变量数据类型之奥秘

&#x1f407;明明跟你说过&#xff1a;个人主页 &#x1f3c5;个人专栏&#xff1a;《Linux &#xff1a;从菜鸟到飞鸟的逆袭》&#x1f3c5; &#x1f516;行路有良友&#xff0c;便是天堂&#x1f516; 目录 一、前言 1、Linux的起源与发展 2、什么是Shell脚本 3、Sh…

MySQL面试——聚簇/非聚簇索引

存储引擎是针对表结构&#xff0c;不是数据库 引擎层&#xff1a;对数据层以何种方式进行组织 update&#xff1a;加索引&#xff1a;行级锁&#xff1b;不加索引&#xff1a;表级锁

LabVIEW专栏七、队列

目录 一、队列范例二、命令簇三、队列应用1.1、并行循环队列1.2、命名队列和匿名队列1.2.1、命名队列1.2.2、匿名队列 1.3、长度为1的队列 队列是一种特殊的线性表&#xff0c;就是队列里的元素都是按照顺序进出。 队列的数据元素又称为队列元素。在队列中插入一个队列元素称为…

HNCTF 2022 week1 题解

自由才是生活主旋律。 [HNCTF 2022 Week1] Interesting_include <?php //WEB手要懂得搜索 //flag in ./flag.phpif(isset($_GET[filter])){$file $_GET[filter];if(!preg_match("/flag/i", $file)){die("error");}include($file); }else{highlight_…

OSPF的协议特性

路由汇总的概念 l 路由汇总&#xff08; Route Aggregation &#xff09;&#xff0c;又称路由聚合&#xff08;Route Summarization&#xff09;&#xff0c;指的是把一组明细路由汇聚成一条汇总路由条目的操作 l 路由汇总能够减少路由条目数量、减小路由表规模&#xff0…

目标检测——3D玩具数据集

在数字化时代&#xff0c;计算机视觉技术取得了长足的进展&#xff0c;其中基于形状的3D物体识别技术更是引起了广泛关注。该技术不仅有助于提升计算机对现实世界物体的感知能力&#xff0c;还在多个领域展现出了广阔的应用前景。本文将探讨基于形状的3D物体识别实验的重要性意…

STM32的Flash读写保护

参考链接 STM32的Flash读写保护&#xff0c;SWD引脚锁的各种解决办法汇总&#xff08;2020-03-10&#xff09;-腾讯云开发者社区-腾讯云 (tencent.com)https://cloud.tencent.com/developer/article/1597959 STM32系列芯片Flash解除写保护的办法 - 知乎 (zhihu.com)https://zh…

Java设计模式:使用责任链模式和状态模式优化‘审批流程‘

Java设计模式&#xff1a;使用责任链模式和状态模式优化审批流程 摘要引言 需求流程图正文内容&#x1f4d0; 基本概念介绍 功能实现示例1:设计模式&#xff1a;责任链模式方法&#xff1a;好处&#xff1a; 示例2:设计模式&#xff1a;责任链模式方法和操作流程&#xff1a;好…

mongodb 分片集群认证

增加认证 副本间认证外部使用认证 如果是开启状态,先关闭路由,再关闭配置服务,最后关闭分片数据复本集中的每个mongod&#xff0c;从次节点开始。直到副本集的所 有成员都离线&#xff0c;包括任何仲裁者。主节点必须是最后一个成员关闭以避免潜在的回滚.最好通过 db.shutdow…

Spring Bean 的生命周期与作用域解析及实战

引言 在Spring框架中&#xff0c;Bean是构成应用的核心组件&#xff0c;它们负责执行应用中的业务逻辑。理解Spring Bean的生命周期和作用域对于开发高效、稳定的Spring应用至关重要。本文将详细解析Spring Bean的生命周期和作用域&#xff0c;并通过实战案例加深理解。 一、…

人工智能好多人都在用,那么用户画像要怎么看?

用户画像是通过对用户行为、偏好、兴趣等数据进行分析和整理&#xff0c;从而形成的关于特定用户群体的描述和模型。在人工智能应用中&#xff0c;用户画像可以起到指导个性化推荐、精准营销、产品设计等方面的作用。以下是用户画像在人工智能应用中的几个重要方面&#xff1a;…

网站被SmartScreen标记为不安全怎么办?

在互联网时代&#xff0c;网站的安全性和可信度是用户选择是否继续访问的重要因素之一&#xff0c;然而&#xff0c;网站运营者偶尔会发现使用Edge浏览器访问网站时&#xff0c;会出现Microsoft Defender SmartScreen&#xff08;以下简称SmartScreen&#xff09;提示网站不安全…

上位机图像处理和嵌入式模块部署(树莓派4b之mcu固件升级)

【 声明&#xff1a;版权所有&#xff0c;欢迎转载&#xff0c;请勿用于商业用途。 联系信箱&#xff1a;feixiaoxing 163.com】 在一个系统当中&#xff0c;可能不止需要树莓派4b一个设备&#xff0c;有的时候还需要搭载一个mcu&#xff0c;做一些运动控制的事情。比如说&…

SRAM控制原理与读写实例

本文对SRAM进行介绍&#xff0c;并对其内部的存储器矩阵、地址译码器、列I/O及I/O数据电路、控制电路、SRAM的读写流程进行简要介绍&#xff0c;并给出SRAM IS62LV256-45U读写实例。 文章目录 存储容量的计算SRAM控制原理SRAM信号线存储器矩阵地址译码器、列I/O及I/O数据电路控…

陆游只爱前妻唐婉,深情大渣男太虐了

陆游和唐婉的感情太好了&#xff0c;经常写诗逗乐。陆游科举考不上&#xff0c;沉迷儿女情长&#xff0c;被母亲拆散。 秦侩当政&#xff0c;就是害死岳飞的那个秦桧。陆游第二次考进士&#xff0c;被秦侩批复“喜论恢复”&#xff0c;没考上。陆游的母亲生气&#xff0c;找个…

计算机视觉——两视图几何求解投影矩阵

上文我提到了通过图像匹配得到基本矩阵&#xff0c;接下来我们要接着求解投影矩阵。 计算投影矩阵思路 假设两个投影矩阵为规范化相机&#xff0c;因此采用基本矩阵进行恢复。在规范化相机下&#xff0c; P [ I ∣ 0 ] P[I|0] P[I∣0], P ′ [ M ∣ m ] P[M|m] P′[M∣m]。…

【Webgl_glslThreejs】搬运分享shader_飘落心形

来源网站 https://www.shadertoy.com/view/4sccWr效果预览 代码演示 将shadertory上的代码转成了threejs可以直接用的代码&#xff0c;引入文件的material&#xff0c;并在创建mesh或已有物体上使用material即可&#xff0c;使用时请注意uv对齐。 import { DoubleSide, Shad…

视频中为什么需要这么多的颜色空间?

在视频处理中&#xff0c;经常会用到不同色彩空间&#xff1a;非线性RGB&#xff0c;线性 RGB&#xff0c;YUV&#xff0c;XYZ……为什么需要这么多的色彩空间呢&#xff1f; 1、视频采集时的线性RGB颜色空间 由数码相机中的 CMOS 传感器产生并写入原始文件&#xff08;Raw Fil…

深度学习检测算法YOLOv5的实战应用

在当前的检测项目中&#xff0c;需要一个高效且准确的算法来处理大量的图像数据。经过一番研究和比较&#xff0c;初步选择了YOLOv5作为算法工具。YOLOv5是一个基于深度学习的检测算法&#xff0c;以其快速和准确而闻名。它不仅能够快速处理图像数据&#xff0c;还能提供较高的…

区块链技术与应用学习笔记(12-13节)——北大肖臻课程

目录 12.BTC-匿名性 一、什么是匿名&#xff1f; 1&#xff0c;有可能破坏比特币匿名性的两个方面 2&#xff0c;如何提高匿名性 一个比特币用户能采用什么样的方法尽量提高个人的匿名性? 分解&#xff1a; 1、网络层怎么提高匿名性? 2、应用层怎么提高匿名性? 零知…
最新文章