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>
相关推荐
Macbethad11 小时前
如何用WPF做工控设置界面
java·开发语言·wpf
csdn_aspnet11 小时前
WPF 做一个简单的电子签名板(一)
c#·wpf
玖笙&11 小时前
✨WPF编程进阶【7.2】:动画类型(附源码)
c++·c#·wpf·visual studio
·心猿意码·20 小时前
WPF中TemplatePart机制详解
wpf
FuckPatience2 天前
WPF 使用UserControl / ContentControl显示子界面
wpf
wangnaisheng2 天前
【WPF】WrapPanel的用法
wpf
源之缘-OFD先行者3 天前
10 万雷达点迹零卡顿回放:WPF + Vortice.Direct2D 多线程渲染实战
wpf
猫林老师3 天前
Flutter for HarmonyOS开发指南(九):测试、调试与质量保障体系
flutter·wpf·harmonyos
LateFrames3 天前
做【秒开】的程序:WPF / WinForm / WinUI3 / Electron
electron·c#·wpf·winform·winui3·claude code
beyond谚语4 天前
第四章 依赖项属性
wpf