WPF 引用动/静态资源示例

文章目录

      • [1. 定义和引用静态资源](#1. 定义和引用静态资源)
        • [1.1 定义资源](#1.1 定义资源)
        • [1.2 引用资源](#1.2 引用资源)
      • [2. 使用外部 ResourceDictionary](#2. 使用外部 ResourceDictionary)
        • [2.1 创建 ResourceDictionary 文件](#2.1 创建 ResourceDictionary 文件)
        • [2.2 引用 ResourceDictionary 文件](#2.2 引用 ResourceDictionary 文件)
        • [2.3 使用资源](#2.3 使用资源)
      • [3. 使用 DynamicResource](#3. 使用 DynamicResource)

在 WPF (Windows Presentation Foundation) 中,引用静态资源(例如颜色、样式、模板等)可以通过多种方式完成,具体取决于你要引用资源的位置和类型。以下是一些常见的方法:

1. 定义和引用静态资源

1.1 定义资源

通常,你会在 ResourceDictionary 中定义静态资源。资源可以定义在 App.xaml 文件中,也可以在单独的 ResourceDictionary 文件中。下面是一个在 App.xaml 中定义静态资源的例子:

html 复制代码
<Application x:Class="YourNamespace.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
        <ResourceDictionary>
            <Color x:Key="MyColor">#FF00FF</Color>
            <Style x:Key="MyButtonStyle" TargetType="Button">
                <Setter Property="Background" Value="{StaticResource MyColor}"/>
                <Setter Property="Foreground" Value="White"/>
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</Application>
1.2 引用资源

在 XAML 文件中引用静态资源时,可以使用 {StaticResource} 标记扩展。例如,在某个 Window 或 UserControl 中引用上面定义的资源:

html 复制代码
<Window x:Class="YourNamespace.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Style="{StaticResource MyButtonStyle}" Content="Click Me"/>
    </Grid>
</Window>

2. 使用外部 ResourceDictionary

可以将资源定义放在单独的 .xaml 文件中,然后在需要的地方进行引用。

2.1 创建 ResourceDictionary 文件

创建一个名为 Themes/Resources.xaml 的文件:

html 复制代码
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Color x:Key="MyColor">#FF00FF</Color>
    <Style x:Key="MyButtonStyle" TargetType="Button">
        <Setter Property="Background" Value="{StaticResource MyColor}"/>
        <Setter Property="Foreground" Value="White"/>
    </Style>
</ResourceDictionary>
2.2 引用 ResourceDictionary 文件

在 App.xaml 中引用该资源文件:

html 复制代码
<Application x:Class="YourNamespace.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Themes/Resources.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>
2.3 使用资源

在窗口或控件中使用资源,与前面的例子一样:

html 复制代码
<Button Style="{StaticResource MyButtonStyle}" Content="Click Me"/>

3. 使用 DynamicResource

DynamicResource 与 StaticResource 类似,但是 DynamicResource 允许在运行时更改资源。如果你需要动态地切换主题或更改样式,这可能会很有用。

html 复制代码
<Button Style="{DynamicResource MyButtonStyle}" Content="Click Me"/>
相关推荐
code_shenbing9 小时前
WPF实现打印机控制及打印
wpf
界面开发小八哥1 天前
界面组件DevExpress WPF中文教程:Grid - 如何显示和隐藏列?
wpf·界面控件·devexpress·ui开发·.net9
虚假程序设计1 天前
python用 PythonNet 从 Python 调用 WPF 类库 UI 用XAML
python·ui·wpf
落落落sss1 天前
MongoDB
数据库·windows·redis·mongodb·微服务·wpf
蒋劲豪1 天前
WPF项目暴露WebApi接口;WinForm项目暴露WebApi接口;C#项目暴露WebApi接口;
开发语言·c#·wpf
狮歌~资深攻城狮2 天前
未来已来:HBase的新功能与发展趋势展望
大数据·wpf·hbase
界面开发小八哥3 天前
界面控件DevExpress WPF v24.2新版亮点:支持.NET 9
.net·wpf·界面控件·devexpress·ui开发·用户界面
九鼎科技-Leo4 天前
WPF快速创建DeepSeek本地自己的客户端-基础思路版本
wpf
MasterNeverDown5 天前
WPF 中为 Grid 设置背景图片全解析
大数据·hadoop·wpf
苏克贝塔5 天前
WPF8-常用控件
wpf