【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>
相关推荐
虫小宝6 分钟前
Java分布式架构下的电商返利APP技术选型与架构设计实践
java·分布式·架构
007php00710 分钟前
百度面试题解析:Zookeeper、ArrayList、生产者消费者模型及多线程(二)
java·分布式·zookeeper·云原生·职场和发展·eureka·java-zookeeper
AsiaLYF15 分钟前
kotlin中MutableStateFlow和MutableSharedFlow的区别是什么?
android·开发语言·kotlin
2501_9160088928 分钟前
iOS 发布全流程详解,从开发到上架的流程与跨平台使用 开心上架 发布实战
android·macos·ios·小程序·uni-app·cocoa·iphone
4Forsee37 分钟前
【Android】浅析 Android 的 IPC 跨进程通信机制
android·java
来旺1 小时前
互联网大厂Java面试全解析及三轮问答专项
java·数据库·spring boot·安全·缓存·微服务·面试
Json____1 小时前
使用node Express 框架框架开发一个前后端分离的二手交易平台项目。
java·前端·express
since �1 小时前
前端转Java,从0到1学习教程
java·前端·学习
詩句☾⋆᭄南笙1 小时前
Mybatis一对一、一对多
java·mybatis·resulttype·resultmap·一对多·一对一
Andya_net1 小时前
Java | 基于redis实现分布式批量设置各个数据中心的服务器配置方案设计和代码实践
java·服务器·分布式