【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>
相关推荐
好好沉淀20 分钟前
Apache 工具包(commons-io commons-lang3 )保姆介绍
java·ide
毕设源码-邱学长24 分钟前
【开题答辩全过程】以 服装购物平台为例,包含答辩的问题和答案
java·eclipse
多喝开水少熬夜35 分钟前
堆相关算法题基础-java实现
java·开发语言·算法
richxu2025100136 分钟前
Java开发环境搭建之 10.使用IDEA创建和管理Mysql数据库
java·ide·intellij-idea
7澄142 分钟前
Java 集合框架:List 体系与实现类深度解析
java·开发语言·vector·intellij-idea·集合·arraylist·linkedlist
行思理42 分钟前
IntelliJIdea 工具新手操作技巧
java·spring·intellijidea
fool_hungry1 小时前
Android MotionEvent ACTION_OUTSIDE 详细解释
android
Adellle1 小时前
Java中同步和异步的区别,以及阻塞和非阻塞的区别
java·开发语言
qq_12498707531 小时前
基于springboot+vue的物流管理系统的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·毕业设计
下位子2 小时前
『OpenGL学习滤镜相机』- Day8: 多重纹理与混合
android·opengl