当前位置: 首页 > news >正文

WPF基础入门-Class8-资源基础

WPF基础入门

Class8-资源基础

前言:方便各种资源的集中管理和动态效果
静态引用:初始化的时候确定样式,后续不发生改变
动态引用:样式随着引用的内容一起改变
1、新建资源字典.xaml,创建一个边框颜色资源MyBrush和一个button的style

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><Style x:Key="DefaultButtonStyle" TargetType="Button"><Setter Property="Foreground" Value="Red"></Setter><Setter Property="FontSize" Value="15"></Setter></Style><SolidColorBrush x:Key="MyBrush" Color="Yellow"/></ResourceDictionary>

2、在App.xaml中引入资源字典

<Application.Resources><!--引入资源字典--><ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="Resources/ButtonStyle.xaml"></ResourceDictionary></ResourceDictionary.MergedDictionaries></ResourceDictionary></Application.Resources>

3、新建一个页面.xaml

<Window x:Class="WPF_Learn.Class_10_资源基础"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_Learn"mc:Ignorable="d"Title="Class_10_资源基础" Height="450" Width="800"><!--在App.xaml里面引入资源字典--><Grid><StackPanel><Button Content="点击我触发Test_Click" Click="Test_Click" Style="{StaticResource DefaultButtonStyle}"></Button><Button Content="静态引用,不会发生改变" Margin="0,5,0,5" BorderBrush="{StaticResource MyBrush}"></Button><Button Content="动态引用,点击按钮MyBrush改变后样式跟着改变" BorderBrush="{DynamicResource MyBrush}"></Button></StackPanel></Grid>
</Window>
 private void Test_Click(object sender, RoutedEventArgs e){// 资源的动态改变 静态引用在初始化的时候固定this.Resources["MyBrush"] = new SolidColorBrush(Colors.Blue);//寻找资源字典//var solidColor = App.Current.FindResource("SolidColor");//var style = App.Current.FindResource("DefaultButtonStyle");}
http://www.lryc.cn/news/160135.html

相关文章:

  • Axure RP PC电商平台Web端交互原型模板
  • Ubuntu目录和linux内核文件用途
  • 更快更强更稳定:腾讯向量数据库测评
  • 【java web】JSP-Java Server Page
  • java文件命令行报错: 找不到或无法加载主类XXX报错及解决
  • 算法训练营day46|动态规划 part08:完全背包 (LeetCode 139. 单词拆分)
  • Java网络编程(二)Socket 套接字(TCP和UDP),以及TCP的回显
  • C++ - 多态语法 - 虚函数使用介绍
  • php获取客户端ip地址及ip所在国家、省份、城市、县区
  • Error: Port Library failed to initialize: -86
  • SOME/IP 支持两种序列化方式:TLV 和 TV
  • Unity之3D物理导航系统
  • 9.4黄金行情是否反转?今日多空如何布局?
  • Win10下使用vim9
  • Flink+Flink CDC版本升级的依赖问题总结
  • Matlab论文插图绘制模板第112期—带阴影标记的图
  • 专业运动耳机哪个牌子好、专业运动耳机推荐
  • 【SQL应知应会】索引 • Oracle版:B-树索引;位图索引;函数索引;单列与复合索引;分区索引
  • 用ChatGPT做一个Chrome扩展 | 京东云技术团队
  • 动态库的制作与使用及 动态库加载失败解决
  • 404 not found nginx(dist打包后,刷新和跳转都是404 not found nginx的问题) 解决方案(打包发布在服务器)
  • 《Chain-of-Thought Prompting Elicits Reasoning in Large Language Models》全文翻译
  • MySQL——笔试测试题
  • WangEditor在Vue前端的应用
  • 初学python的感受
  • SpringSecurity中注解讲解
  • 朔雪流量复制器的前端
  • Anaconda下 Prophet的安装,pystan和fbprophet的版本问题
  • IntelliJ IDEA 简介
  • 20230904 QT客户端服务器搭建聊天室