wpf中资源的使用

前言

在 WPF 中,"资源"允许你定义可重用的对象(如画笔、样式、模板、数据等),并在应用程序的不同部分甚至整个应用程序中引用它们,这有助于实现一致性、可维护性和 XAML 代码的简洁性。

1、应用程序级别的资源

将资源文件添加到App.xaml中,应用程序级别的资源可以在该程序的多个窗体中使用

csharp 复制代码
<Application x:Class="wpf资源.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:wpf资源"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <SolidColorBrush x:Key="GlobalBackgroundBrush" Color="Red" />
        <Style x:Key="GlobalButtonStyle" TargetType="Button">
            <Setter Property="FontSize" Value="14"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="Background" Value="Blue"/>
        </Style>
    </Application.Resources>
</Application>

1)在MainWindow中调用GlobalButtonStyle

csharp 复制代码
 <Button Height="100" Content=" 123"  Style="{StaticResource GlobalButtonStyle}" />

运行结果:

2)在Window1中调用GlobalButtonStyle

csharp 复制代码
 <Button Height="100" Content=" 123"  Style="{StaticResource GlobalButtonStyle}" />

运行结果:

2、窗体级别的资源

在该窗体以及其子元素使用

csharp 复制代码
<Window.Resources>
            <Style x:Key="MainWindowlTextBlockStyle" TargetType="TextBlock">
                <Setter Property="FontSize" Value="14"/>
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="Background" Value="Green" />
            </Style>
    </Window.Resources>

在MainWindow中调用MainWindowlTextBlockStyle

csharp 复制代码
 <TextBlock Height="100" Text =" 456"  Style="{StaticResource ResourceKey= MainWindowlTextBlockStyle}" />

3、元素级别的资源

只能在该元素以及其子元素中使用

csharp 复制代码
 <Grid>
        <Grid.Resources >
            <SolidColorBrush x:Key="PanelHighlightBrush" Color="Yellow"/>
        </Grid.Resources >
        <StackPanel Orientation="Vertical" >
            <TextBlock Height="100" Text =" 456"  Background ="{StaticResource ResourceKey= PanelHighlightBrush}" />
        </StackPanel >
    </Grid>

4、元素级别的资源

外部资源字典

1)添加外部资源

MainWindow.xaml中添加以下代码,代码在Window.Resources中定义了一个ResourceDictionary资源字典,将需要添加的外部资源全都放到ResourceDictionary.MergedDictionaries中,格式如:"pack://application:,/外部命名控件名;component/Dictionary1.xaml",如果component后面还跟了文件夹的话,则这个路径也要加上文件夹名。这里的代码命名控件名是WpfApp2,由于命名空间内直接包含了Dictionary1.xaml,所以component后面跟的是Dictionary1.xaml。

2)添加Window窗体级别的资源

Window窗体级别的资源放到ResourceDictionary.MergedDictionaries外部和ResourceDictionary内部,比如下面的ButtonColor。

csharp 复制代码
<Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source = "pack://application:,,,/WpfApp2;component/Dictionary1.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <SolidColorBrush x:Key="ButtonColor" Color="red"/>
        </ResourceDictionary>
    </Window.Resources>

MainWindow调用

csharp 复制代码
<Button Height="100"  Background ="{StaticResource ResourceKey=Color_Active}" />

马工撰写的年入30万+C#上位机项目实战必备教程(点击下方链接即可访问文章目录)

1、《C#串口通信从入门到精通》

2、《C#与PLC通信从入门到精通 》

3、《C# Modbus通信从入门到精通》

4、《C#Socket通信从入门到精通 》

5、《C# MES通信从入门到精通》

6、《winform控件从入门到精通》

7、《C#操作MySql数据库从入门到精通》

相关推荐
weixin_537590451 天前
《C程序设计语言》练习答案(练习1-13)
c语言·开发语言·c#
a17798877121 天前
小程序上传图像失败
小程序·c#
JosieBook1 天前
【C#】C# 所有关键字总结
开发语言·算法·c#
我是唐青枫1 天前
C#.NET ConcurrentStack<T> 深入解析:无锁栈原理、LIFO 语义与使用边界
网络·c#·.net
黑棠会长1 天前
ABP框架09.数据安全与合规:审计日志与实体变更追踪
分布式·安全·架构·c#·abp
JMchen1231 天前
Android NDK开发从入门到实战:解锁应用性能的终极武器
android·开发语言·c++·python·c#·android studio·ndk开发
IT小哥哥呀1 天前
基于windows的个人/团队的时间管理工具
windows·c#·wpf·时间管理
JosieBook1 天前
【C#】C# 访问修饰符与类修饰符总结大全
前端·javascript·c#
星夜泊客1 天前
C# : 引用类型都存在堆上吗
unity·c#
chiwei_hua1 天前
如何在 Blazor Web 前端中使用 C# 进行数据交互?
前端·c#·交互