【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>
相关推荐
Rinleren2 分钟前
企业级 K8s 运维实战:集群搭建、微服务暴露(Ingress)、监控告警(Prometheus)全流程
java·容器·kubernetes
熙客11 分钟前
Session与JWT安全对比
java·分布式·安全·web安全·spring cloud
豆豆豆大王15 分钟前
Android studio图像视图和相对布局知识点
android·ide·android studio
花心蝴蝶.37 分钟前
JVM 垃圾回收
java·jvm·算法
寒月霜华38 分钟前
java-File
java·开发语言
yujkss1 小时前
23种设计模式之【抽象工厂模式】-核心原理与 Java实践
java·设计模式·抽象工厂模式
我命由我123451 小时前
Android 实例 - Android 圆形蒙版(Android 圆形蒙版实现、圆形蒙版解读)
android·java·java-ee·android studio·安卓·android-studio·android runtime
咖啡Beans1 小时前
SpringBoot集成Clickhouse
java·spring boot
天若有情6732 小时前
【Android】Android项目目录结构及其作用
android