WPF之Style

样式分为静态样式和动态样式

静态样式【StaticResource 】:编译后就不能修改样式了

动态样式【DynamicResource 】:编译后可以修改样式,可以用于主题切换设置

实例效果:

对应的xmal代码:

cs 复制代码
<Window x:Class="Wpf_Style.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:Wpf_Style"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <Window.Resources>
        <!--定义一个样式基类-->
        <Style  TargetType="Button">
            <Setter Property="Background" Value="Blue"/>
            <Setter Property="FontSize" Value="30"/>
            <Setter Property="Height" Value="50"/>
            <Setter Property="Width" Value="200"/>
            <Setter Property="Margin" Value="20,10"/>
        </Style>
        <!--继承基类样式后,拓展新样式-->
        <Style x:Key="login_style" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
            <Setter Property="Background" Value="green"/>

        </Style>
        <!--继承基类样式后,拓展新样式-->
        <Style x:Key="quit_style" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
            <Setter Property="Background" Value="Red"/>
            
        </Style>
    </Window.Resources>
    <StackPanel>
        <Button  Style="{StaticResource login_style}"  Content="登录"  />
        <Button Style="{StaticResource quit_style}"  Content="退出" />
        <Button Content="忘记密码"/>
    </StackPanel>
</Window>
相关推荐
LcVong18 小时前
WPF MediaPlayer获取网络视频流当前帧并展示图片完整范例
网络·wpf
bugcome_com19 小时前
WPF数据绑定入门:从传统事件到5种绑定模式
wpf
LateFrames21 小时前
我用 WPF 做了一个 “苍蝇飞舞” 的屏保
ui·wpf
wuty0071 天前
完善基于WPF开发的标尺控件(含实例代码)
wpf·wpf标尺·支持横向竖向标尺·ruler
浩浩测试一下2 天前
洪水猛兽攻击 Ddos Dos cc Drdos floods区别
安全·web安全·网络安全·系统安全·wpf·可信计算技术·安全架构
无心水2 天前
分布式环境下定时任务与SELECT FOR UPDATE的陷阱与解决方案
分布式·后端·wpf·xxl-job·quartz·定时任务·selectforupdate
xdpcxq10292 天前
Spring AOP + Guava RateLimiter 用注解实现优雅限流
spring·wpf·guava
Aevget3 天前
界面控件DevExpress WPF v25.2新版亮点:模板工具包全新升级
wpf·界面控件·devexpress·ui开发·.net 10
czhc11400756633 天前
wpf 129
wpf
码界奇点4 天前
基于eBPF技术的高性能网络防火墙系统设计与实现
开发语言·网络·毕业设计·php·wpf·go语言·源代码管理