WPF 静态样式与动态样式的定义及使用详解

一、静态样式的定义方式

静态样式是在 XAML 中直接定义的样式资源,加载时一次性解析,运行时修改不会影响界面显示。

  1. 全局静态样式(App.xaml 中定义)

    作用范围:整个应用程序所有窗口和控件

    复制代码
    <!-- App.xaml -->
    <Application x:Class="WpfApp1.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="MainWindow.xaml">
        <Application.Resources>
            <!-- 全局按钮样式(带Key,需显式引用) -->
            <Style x:Key="GlobalButtonStyle" TargetType="Button">
                <Setter Property="Background" Value="#2c3e50"/>
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="Padding" Value="10,5"/>
            </Style>
            
            <!-- 全局文本框样式(无Key,自动应用于所有TextBox) -->
            <Style TargetType="TextBox">
                <Setter Property="BorderBrush" Value="#bdc3c7"/>
                <Setter Property="Margin" Value="5"/>
                <Setter Property="Padding" Value="5"/>
            </Style>
        </Application.Resources>
    </Application>
  2. 局部静态样式(在容器中定义)

    作用范围:仅当前容器及其子元素

    复制代码
    <!-- MainWindow.xaml -->
    <Window x:Class="WpfApp1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="静态样式示例" Height="300" Width="400">
        
        <!-- 窗口级局部样式 -->
        <Window.Resources>
            <Style x:Key="WindowLevelLabelStyle" TargetType="Label">
                <Setter Property="FontSize" Value="14"/>
                <Setter Property="Foreground" Value="#34495e"/>
            </Style>
        </Window.Resources>
        
        <StackPanel Margin="10">
            <!-- 面板级局部样式 -->
            <StackPanel.Resources>
                <Style x:Key="PanelLevelButtonStyle" TargetType="Button">
                    <Setter Property="Background" Value="#e74c3c"/>
                    <Setter Property="Foreground" Value="White"/>
                </Style>
            </StackPanel.Resources>
            
            <!-- 控件级局部样式(在具体控件内部定义) -->
            <TextBlock>
                <TextBlock.Resources>
                    <Style TargetType="TextBlock">
                        <Setter Property="TextDecorations" Value="Underline"/>
                    </Style>
                </TextBlock.Resources>
            </TextBlock>
        </StackPanel>
    </Window>
二、静态样式的使用方法

静态样式通过{StaticResource 资源Key}语法引用:

复制代码
<!-- 使用全局样式 -->
<Button Style="{StaticResource GlobalButtonStyle}" Content="使用全局样式"/>
​
<!-- 使用窗口级局部样式 -->
<Label Style="{StaticResource WindowLevelLabelStyle}" Content="使用窗口样式"/>
​
<!-- 使用面板级局部样式 -->
<Button Style="{StaticResource PanelLevelButtonStyle}" Content="使用面板样式"/>
​
<!-- 无Key的样式会自动应用于所有同类型控件 -->
<TextBox Text="自动应用全局文本框样式"/>
三、动态样式的定义与使用

动态样式与静态样式定义方式相同,但引用方式不同,且支持运行时更新:

  1. 动态样式的定义(与静态样式相同)

    复制代码
    <Window.Resources>
        <SolidColorBrush x:Key="DynamicBrush" Color="Green"/>
        
        <Style x:Key="DynamicTextStyle" TargetType="TextBlock">
            <Setter Property="Foreground" Value="{DynamicResource DynamicBrush}"/>
            <Setter Property="FontSize" Value="16"/>
        </Style>
    </Window.Resources>
  2. 动态样式的使用(使用 DynamicResource)

    复制代码
    <TextBlock Style="{DynamicResource DynamicTextStyle}" Text="动态样式文本"/>
    <Button Click="ChangeDynamicStyle">更改动态样式</Button>
  3. 运行时修改动态样式(C# 后台代码)

    复制代码
    private void ChangeDynamicStyle(object sender, RoutedEventArgs e)
    {
        // 修改动态资源,界面会实时更新
        this.Resources["DynamicBrush"] = new SolidColorBrush(Colors.Blue);
        
        // 也可以直接修改样式
        var newStyle = new Style(typeof(TextBlock));
        newStyle.Setters.Add(new Setter(TextBlock.ForegroundProperty, Brushes.Red));
        newStyle.Setters.Add(new Setter(TextBlock.FontSizeProperty, 20.0));
        this.Resources["DynamicTextStyle"] = newStyle;
    }
四、静态样式与动态样式的核心区别
特性 静态样式(StaticResource) 动态样式(DynamicResource)
解析时机 加载时一次性解析 首次使用时解析,之后动态监听
性能 较好(无需持续监听) 略低(需要维护监听机制)
运行时修改 不支持(修改后界面无变化) 支持(修改后界面实时更新)
适用场景 样式固定不变的场景 需要动态切换主题、样式的场景
五、使用注意事项
  1. 静态样式引用的资源必须在 XAML 中定义在引用位置之前

  2. 动态样式可以引用在定义位置之后的资源

  3. 无 Key 的样式(仅指定 TargetType)会自动应用于该类型的所有控件,称为 "隐式样式"

  4. 当同时存在多个作用范围的样式时,优先级为:行内样式 > 局部样式 > 全局样式

通过合理选择静态或动态样式,可以在性能和灵活性之间取得平衡,满足不同场景的需求。

相关推荐
淡海水17 小时前
07-04-源码-.NET Core GC清扫与压缩
unity·架构·c#·.netcore·gc·1024程序员节
zwm2698888151 天前
hd 3#负一西送风,能远程启不能远程停
c#
Ai-_Man1 天前
Meta Muse还是不可以导出左侧所有对话,只能点进去单条内容导出,方法不对吗?
开发语言·人工智能·ai·小程序·c#·1024程序员节
2401_885885041 天前
国际语音C#接口怎么写?.NET环境下实现国际语音验证码批量下发
开发语言·c#·.net
xy34531 天前
Axure 9.0 列表框 介绍
ui·axure·原型·产品设计
xy34531 天前
Axure 9.0 内联框架加载方式
前端·ui·html·axure·原型·产品设计
特立独行的猫a2 天前
仓颉语言cj-tauri 跨端开发框架实战:给鸿蒙侧加一个「读设备电量」的原生能力
ui·harmonyos·tauri·1024程序员节·仓颉·鸿蒙pc
淡海水2 天前
07-03-源码-.NET Core GC标记与扫描
unity·c#·.netcore·gc
zzu123zsw2 天前
当 GPU 加速渲染遇上无头组件:拆解 Yororen UI 在 GPUI 生态下的状态解耦与 XML 模板实践
xml·ui·rust·gui·gpui
波力海苔夹心脆6752 天前
C# string 字符串方法大全:判空、查找、截取、分割、替换一篇讲透
经验分享·c#·.net·字符串·string·入门教程