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>
相关推荐
rollingman9 小时前
WPF GroupBox 淡入淡出
wpf
yy76349666814 小时前
WPF 之 简单高效的Revit多语言支持方案
java·大数据·linux·服务器·wpf
张人玉1 天前
WPF 触发器详解:定义、种类与示例
c#·wpf·1024程序员节·布局控件
rollingman1 天前
WPF ComboBox 样式
wpf·combobox·textbox
Aevget1 天前
DevExpress WPF中文教程:Data Grid - 如何使用虚拟源?(三)
wpf·界面控件·devexpress·ui开发·1024程序员节
张人玉1 天前
WPF 核心概念笔记(补充示例)
c#·wpf·1024程序员节·布局控件
亦陈不染2 天前
WPF-MVVM的简单入门(第一个MVVM程序)
wpf
FuckPatience2 天前
WPF TreeView 条目右键命令 ContextMenu按钮绑定命令
wpf
The 旺2 天前
【案例实战】HarmonyOS分布式购物车:多设备无缝协同的电商体验
分布式·wpf·harmonyos