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>
相关推荐
bugcome_com12 小时前
WPF样式进阶实战:外置样式+MVVM主题切换+样式优先级全解析
c#·.net·wpf
lalala_Zou20 小时前
场景题:电商平台订单未支付过期如何实现自动关闭订单?
wpf
czhc114007566320 小时前
wpf 16
wpf
cn_mengbei2 天前
鸿蒙PC原生应用开发实战:ArkTS与DevEco Studio从零构建跨端桌面应用全栈指南
华为·wpf·harmonyos
lingxiao168882 天前
WebApi详解+Unity注入--上篇:基于Framework的WebApi
c#·wpf·web
是一个Bug2 天前
Java后端开发面试题清单(50道) - 分布式基础
java·分布式·wpf
无心水2 天前
【分布式利器:腾讯TSF】4、TSF配置中心深度解析:微服务动态配置的终极解决方案
分布式·微服务·架构·wpf·分布式利器·腾讯tsf·分布式利器:腾讯tsf
lingxiao168882 天前
WebApi详解+Unity注入--下篇:Unity注入
unity·c#·wpf
无心水3 天前
【分布式利器:腾讯TSF】6、TSF可观测性体系建设实战:Java全链路Metrics+Tracing+Logging落地
java·分布式·架构·wpf·分布式利器·腾讯tsf·分布式利器:腾讯tsf
故事不长丨4 天前
C#字典(Dictionary)全面解析:从基础用法到实战优化
开发语言·c#·wpf·哈希算法·字典·dictionary·键值对