WPF中的“资源”

资源概述

WPF中的资源的概念有点类似 web 技术中的静态资源的概念。可以是一个样式,也可以是一个button的边框设置集合。

可以简单的将资源分为如下几个类别:

  • 窗体资源:顾名思义,仅可在当前窗体中使用
  • 全局资源:相对于窗体资源而言,是一个全局级别的,可以被多个窗体引用,可以根据不同的维度定义多个全局资源文件
  • 动态资源:“值”可以被改变的资源,例如:程序启动的时候button的边框是红色的,当点击某个其他按钮后,将边框变成蓝色

窗体资源

创建

  <Window.Resources>
        <SolidColorBrush x:Key="SolidColor"  Color="Red">
        </SolidColorBrush>
    </Window.Resources>

引用

使用花括号和关键字 StaticResource

 <StackPanel>
        <Button Content="我是一个按钮" Margin="10"  BorderBrush="{StaticResource  SolidColor}"></Button>
 </StackPanel>

全局资源

创建 资源字典 文件

右键工程,点击添加-资源字典,命名为  DictionaryButton.xaml

编写全局资源

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <SolidColorBrush x:Key="GlobalSolidColor" Color="Yellow"></SolidColorBrush>
    <Style x:Key="DefaultButtonStyle" TargetType="Button">
        <Setter   Property="Foreground"  Value="Blue"  ></Setter>
        <Setter   Property="FontSize"  Value="20"  ></Setter>
        <Setter  Property="BorderBrush" Value="Pink" ></Setter>
        <Setter   Property="BorderThickness"  Value="10"  ></Setter>
    </Style>
</ResourceDictionary>

引入

在 App.xaml 文件中引入全局资源文件 DictionaryButton.xaml

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="DictionaryButton.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

引用

   <StackPanel>
        <Button Content="我是一个按钮" Margin="10"   Style="{StaticResource DefaultButtonStyle}"></Button>
   </StackPanel>

动态资源

就资源本身而言,动态资源并没有什么特殊之处,仅仅是在处理方式上面的差异。

创建

参考 窗体资源

引用

                <StackPanel>
                    <Button Content="改变下面控件的边框颜色" Margin="10"  Click="Button_Click" ></Button>
                    <Button Content="我是一个按钮" Margin="10"  BorderBrush="{DynamicResource  SolidColor}" BorderThickness="10"></Button>
                </StackPanel>

动态编辑资源

   private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Resources["SolidColor"] = new SolidColorBrush(Colors.Blue);
        }

代码

https://github.com/Naylor55/WPF-Taste/tree/main/resource/ResourceTaste

最新文章

  1. Timequest收集命令
  2. github with msysgit:配置SSH Key
  3. linux下文件搜索命令学习笔记
  4. css3 倒影
  5. win7连接共享打印机 错误为
  6. linux mount命令的用法详细解析
  7. Linux系统自启动脚本
  8. 一个简单的获取参数的jqure
  9. Ubuntu系统下为IDEA创建启动图标
  10. c语言,动态数组
  11. Tiny6410之按键裸机驱动
  12. c# 第一节课 一些简单的应用
  13. HTML之事件处理程序
  14. [Reinforcement Learning] Cross-entropy Method
  15. python之路day02--格式化输出、初始编码、运算符
  16. Java RSA 公钥加密私钥解密
  17. android使用smack实现简单登录功能
  18. &#127827; react16.2新特性 &#127827;
  19. js实现sleep
  20. RabbitMQ下的生产消费者模式与订阅发布模式

热门文章

  1. 苹果手机安装郑好办手机app后给绿城通公交卡充值的步骤
  2. 在 Linux 中找出 CPU 占用高的进程
  3. C#-7 结构和枚举
  4. 空 Maven项目转成 Web项目 &amp; SpringMVC调用其他 Module中的方法可能会遇到的小问题
  5. MergeOption.NoTracking的使用
  6. 华为路由器NAT基本配置命令
  7. 代码块及final关键字的使用
  8. [Go疑难杂症]为什么nil不等于nil
  9. JS逆向实战6-- x轴 y轴 过点触验证码
  10. The Google File System 翻译和理解