【WPF】WPF(样式)

Window.Resources 当前窗体作用域资源

TargetType="Button"

复制代码
  使得当前窗体的组件类型都适配此样式
xml 复制代码
    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="Background" Value="WhiteSmoke"></Setter>
            <Setter Property="Margin" Value="20,10"></Setter><!--左右20 上下10-->
        </Style>
    </Window.Resources>
        <StackPanel>
        <Button Content="登录"  />
        <Button Content="退出"/>
        <Button Content="忘记密码" />
        <Button Content="忘记密码" />
    </StackPanel>

x:Key="loginStyle" 指定样式

xml 复制代码
    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="Background" Value="WhiteSmoke"></Setter>
            <Setter Property="Margin" Value="20,10"></Setter><!--左右20 上下10-->
        </Style>
        <Style x:Key="QuitStyle" TargetType="Button">
            <Setter Property="Background" Value="Red"></Setter>
            <Setter Property="Width" Value="200"></Setter>
            <Setter Property="Height" Value="50"></Setter>
        </Style>
    </Window.Resources>
    <StackPanel>
        <Button Content="登录"  />
        <Button Content="退出"  Style="{StaticResource QuitStyle}"/>
        <Button Content="忘记密码" />
        <Button Content="忘记密码" />
    </StackPanel>

BasedOn="{ StaticResource {x:Type Button}}" 继承样式

复制代码
  继承通用样式后设置样式会覆盖继承的样式
xml 复制代码
<Window x:Class="WpfTest.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:WpfTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="Background" Value="WhiteSmoke"></Setter>
            <Setter Property="Margin" Value="20,10"></Setter><!--左右20 上下10-->
        </Style>
        <Style x:Key="loginStyle" TargetType="Button" BasedOn="{ StaticResource {x:Type Button}}"> <!--继承通用设置-->
            <Setter Property="Background" Value="Green"></Setter>
            <!--覆盖通用设置-->
        </Style>
        <Style x:Key="QuitStyle" TargetType="Button">
            <Setter Property="Background" Value="Red"></Setter>
            <Setter Property="Width" Value="200"></Setter>
            <Setter Property="Height" Value="50"></Setter>
        </Style>
    </Window.Resources>
    <StackPanel>
        <Button Content="登录" Style="{StaticResource loginStyle}" />
        <!--指定style-->
        <Button Content="退出"/>
        <Button Content="忘记密码" />
        <Button Content="忘记密码" />
    </StackPanel>
</Window>
相关推荐
逻辑驱动的ken14 小时前
Java高频面试场景题07
java·开发语言·面试·职场和发展·求职招聘·春招
slarymusic14 小时前
解决报错net.sf.jsqlparser.statement.select.SelectBody
java
callJJ14 小时前
JVM 内存区域划分详解——从生活比喻到运行时数据区全景图
java·jvm·面试·内存区域划分
y小花14 小时前
安卓USB服务概述
android·usb
小江的记录本14 小时前
【网络安全】《网络安全与数据安全核心知识体系》(包括数据脱敏、数据加密、隐私合规、等保2.0)
java·网络·后端·python·算法·安全·web安全
北漂Zachary14 小时前
PHP vs Python vs Java:三大编程语言终极对比
java·python·php
Paxon Zhang14 小时前
JavaEE初阶学习web开发的第一步**计算机组成原理,操作系统,进程(基础扫盲)**
java·后端·学习·java-ee
慕容卡卡14 小时前
大模型核心,MCP(模型上下文协议)和Session API
java·开发语言·人工智能·spring boot·spring cloud
zore_c14 小时前
【C++】C++类和对象实现日期类项目——时间计算器!!!
java·c语言·数据库·c++·笔记·算法·排序算法
小郑加油14 小时前
python学习Day6-7天:条件判断与基本综合应用
java·服务器·apache