泉州网站建设网站建设,wordpress子站点解析,农业网站模板,长沙推广优化公司WPF基础入门
Class8-资源基础
前言#xff1a;方便各种资源的集中管理和动态效果 静态引用#xff1a;初始化的时候确定样式#xff0c;后续不发生改变 动态引用#xff1a;样式随着引用的内容一起改变 1、新建资源字典.xaml#xff0c;创建一个边框颜色资源MyBrush和一…WPF基础入门
Class8-资源基础
前言方便各种资源的集中管理和动态效果 静态引用初始化的时候确定样式后续不发生改变 动态引用样式随着引用的内容一起改变 1、新建资源字典.xaml创建一个边框颜色资源MyBrush和一个button的style
ResourceDictionary xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlStyle x:KeyDefaultButtonStyle TargetTypeButtonSetter PropertyForeground ValueRed/SetterSetter PropertyFontSize Value15/Setter/StyleSolidColorBrush x:KeyMyBrush ColorYellow//ResourceDictionary2、在App.xaml中引入资源字典
Application.Resources!--引入资源字典--ResourceDictionaryResourceDictionary.MergedDictionariesResourceDictionary SourceResources/ButtonStyle.xaml/ResourceDictionary/ResourceDictionary.MergedDictionaries/ResourceDictionary/Application.Resources3、新建一个页面.xaml
Window x:ClassWPF_Learn.Class_10_资源基础xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WPF_Learnmc:IgnorabledTitleClass_10_资源基础 Height450 Width800!--在App.xaml里面引入资源字典--GridStackPanelButton Content点击我触发Test_Click ClickTest_Click Style{StaticResource DefaultButtonStyle}/ButtonButton Content静态引用不会发生改变 Margin0,5,0,5 BorderBrush{StaticResource MyBrush}/ButtonButton Content动态引用点击按钮MyBrush改变后样式跟着改变 BorderBrush{DynamicResource MyBrush}/Button/StackPanel/Grid
/Windowprivate 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);}