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>
相关推荐
一念春风19 小时前
证件照制作工具(WPF C#)
c#·wpf
He BianGu2 天前
【笔记】在WPF中GiveFeedbackEventHandler的功能和应用场景详细介绍
笔记·wpf
就是有点傻2 天前
WPF自定义控件-水晶球
wpf
He BianGu2 天前
【笔记】在WPF中QueryContinueDragEvent的详细介绍
笔记·wpf
He BianGu2 天前
【笔记】在WPF中QueryCursor事件的功能和应用场景详细介绍
笔记·wpf
He BianGu2 天前
【笔记】在WPF中CommandManager的功能和应用场景详细介绍
笔记·wpf
关关长语2 天前
HandyControl中Button图标展示多色路径
c#·.net·wpf·handycontrol
baivfhpwxf20233 天前
WPF DataGrid 指定列的数据可以编辑功能开发
wpf
求学中--4 天前
万物互联的钥匙:HarmonyOS SDK 深度解析与实战指南
wpf
武藤一雄4 天前
WPF Command 设计思想与实现剖析
windows·微软·c#·.net·wpf·.netcore