首页 > 其他分享 >wpf 资源字典 换肤

wpf 资源字典 换肤

时间:2023-07-02 20:55:54浏览次数:35  
标签:换肤 Style button FindResource window wpf 资源 字典

介绍一下wpf中给控件更改样式的集中方法,只用button演示,其他控件相同

1.使用代码更改button的style

定义button的style1

<Style TargetType="Button" x:Key="buttonstyle1">
            <Setter Property="Background" Value="LightBlue" />
            <Setter Property="Foreground" Value="Black" />
            <Setter Property="FontSize" Value="14" />
            <Setter Property="Margin" Value="10" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="SkyBlue" />
                    <Setter Property="Foreground" Value="White" />
                    <Setter Property="FontSize" Value="16" />
                </Trigger>
            </Style.Triggers>
        </Style>

定义button的style2

<Style TargetType="Button" x:Key="buttonstyle2">
            <Setter Property="Background" Value="LightGreen" />
            <Setter Property="Foreground" Value="Black" />
            <Setter Property="FontSize" Value="14" />
            <Setter Property="Margin" Value="10" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="LightSeaGreen" />
                    <Setter Property="Foreground" Value="Red" />
                    <Setter Property="FontSize" Value="16" />
                </Trigger>
            </Style.Triggers>
        </Style>

button引用style:

<Grid>
        <StackPanel>
            <Button Name="btn1" Content="buttonstyle1" Style="{StaticResource buttonstyle1}"/>
            <Button Name="btn2" Content="buttonstyle1" Style="{StaticResource buttonstyle1}"/>
            <Button Name="btn3" Content="buttonstyle1" Style="{StaticResource buttonstyle1}"/>
            <Button Name="btn4" Content="切换样式方式1" Click="btn4_Click" />
        </StackPanel>
    </Grid>

代码:在代码中通过button的name操作对应的控件对象,使用FindResource方法从资源中根据资源的key去查找对应的资源(2个style是定义在window.reource中的)

 private void btn4_Click(object sender, RoutedEventArgs e)
        {
            if (btn1.Style == (Style)FindResource("buttonstyle1"))
            {
                btn1.Style = (Style)FindResource("buttonstyle2");
                btn2.Style = (Style)FindResource("buttonstyle2");
                btn3.Style = (Style)FindResource("buttonstyle2");
            }
            else if (btn1.Style == (Style)FindResource("buttonstyle2"))
            {
                btn1.Style = (Style)FindResource("buttonstyle1");
                btn2.Style = (Style)FindResource("buttonstyle1");
                btn3.Style = (Style)FindResource("buttonstyle1");
            }
        }

这种方式可以实现功能,但是需要用到button的name才可以,比较麻烦。所以演化出了第二种方法

2.使用资源字典

2.1第一步:定义2个资源字典,每个资源字典中放一个style

  把上面的2个style放入资源字典,并且不屑key,让它自动应用于所有的button

 dictionary1:

  dictionary2:

 2.2 在window的resource中或者app.xaml中引用任意一个资源字典

  写在window的resource中,则这个window中的所有button会自动引用,如果想要其他的window也引用,需要写在app.xaml中

window.resource

 app.xaml

 2.3代码更改引用的资源字典

private void Button_Click(object sender, RoutedEventArgs e)
        {
             //第二种方法:改变资源字典的引用(使用场景:一个Dictionary中定义了很多种类控件的style)
            // 1.获取当前窗口资源
            ResourceDictionary resources = this.Resources;
            // 2.查找第一个资源字典
            ResourceDictionary firstResourceDictionary = resources.MergedDictionaries[0];

            // 3.创建第二个资源字典
            ResourceDictionary secondResourceDictionary = new ResourceDictionary();
            secondResourceDictionary.Source = new Uri("Dictionary2.xaml", UriKind.RelativeOrAbsolute);

            // 4.替换第一个资源字典为第二个资源字典
            resources.MergedDictionaries.Remove(firstResourceDictionary);
            resources.MergedDictionaries.Insert(0, secondResourceDictionary);
        }

注解:

在 1.获取当前窗口资源使用代码:

ResourceDictionary resources = this.Resources;
在app.xaml中获取资源使用代码:
 //ResourceDictionary resources = Application.Current.Resources;

在 2.查找第一个资源字典这一步中,查找到的资源字典就是window.resource中引用的资源字典(Dictionary1.xaml)

在4.4.替换第一个资源字典为第二个资源字典这一步中,Insert的第一个参数是0?

解释:使用MergedDictionaries时可以合并很多个资源字典,这里的排布顺序类似于一个数组,可以按照下标访问

<Window.Resources>
        <!--资源字典的引用可放在app.xaml中-->
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary   Source="Dictionary1.xaml"/>
                <ResourceDictionary   Source="TreeViewDictionary.xaml"/>
                <ResourceDictionary   Source="TabcontrolDictionary.xaml"/>
                <ResourceDictionary   Source="textBlockDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
    </Window.Resources>

 

标签:换肤,Style,button,FindResource,window,wpf,资源,字典
From: https://www.cnblogs.com/1024E/p/17516421.html

相关文章

  • Redis数据结构——字典
    前言字典在Redis中的应用非常广泛,数据库与哈希对象的底层实现就是字典。一、复习散列表1.1散列表散列表(哈希表),其思想主要是基于数组支持按照下标随机访问数据时间复杂度为O(1)的特性。可以说是数组的一种扩展。假设,我们为了方便记录某高校数学专业的所有学生的信息。要求可......
  • WPF复习知识点记录
    WPF复习知识点记录由于近几年主要在做Web项目,客户端的项目主要是以维护为主,感觉对于基础知识的掌握没有那么牢靠,趁着这个周末重新复习下WPF的相关知识。文章内容主要来自大佬刘铁锰老师的经典著作《深入浅出WPF》。因为是复习,所以知识内容不会一一记录,如有需要了解更多可以看书......
  • 用Wpf做一个Block编程画板(续5-Diagram画板,仿Scratch)
    用Wpf做一个Block编程画板(续5-Diagram画板)先上一张效果动图,本次更新主要仿照Scratch,目前仅完成拖拽部分,逻辑部分后续完善。同样老规矩,先上源码地址:https://gitee.com/akwkevin/aistudio.-wpf.-diagram本次扩展主要内容:1.Block模块,入口在文件新建下。2.简易Block的使用:......
  • python 字典key单引号变双引号
    背景:str1="{'a':1,'b':2,'c':3}"把字典格式的字符串str1转成字典importjsons_dic=json.loads(str1)报错信息:json.decoder.JSONDecodeError:Expectingpropertynameenclosedindoublequotes:line1column2(char1) 解决思路:方法一:(不建......
  • Silverlight资源字典
     资源字典是一个受ResourceDictionary类支持的概念。资源字典是一个键控对象字典,可在XAML和代码中使用。XAML是最常见的用法,特别是用于最初定义资源字典中的对象。资源字典可存在于应用程序结构中的多个位置,包括作为直接(页)资源、应用程序资源(作为Application对象的一部分)或......
  • [WPF]静态资源(StaticResource)和动态资源(DynamicResource)
    一、文章概述本演示介绍了WPF的静态资源和动态资源的基本使用,并对两者做了简单的比较。静态资源(StaticResource)指的是在程序载入内存时对资源的一次性使用,之后就不再访问这个资源了;动态资源(DynamicResource)使用指的是在程序运行过程中然会去访问资源。相关下载(代码、屏幕录像):h......
  • wpf checkbox控件模板
    先看一下上一篇文章:wpfbutton控件模板。再看此文会更好理解  vs生产的checkbox控件模板有很长,我们直接找到controltemplate标签,和controltemplate.triggers。控件模板都先找这两个地方看一下布局和触发器既可以了。剩下的都是定义的一些资源和样式。checkbox的控件模板中定......
  • wpf button控件模板
    1.从button的style说起button有很多属性,我们在xaml中定义一个按钮时可以指定button的content、background、height、width等等。这些都是button的属性,而style也不例外也是button的一个属性,只是在给style属性赋值时不能简单的像height=“100”一样简单的设定一个字符串。先看......
  • WPF Showdialog与DialogResult的注意事项
    我们知道如果要设置window.Showdialog()的返回值,需要设置window.DialogResult属性。但是设置window.DialogResult属性会自动触发close,如果window.DialogResult属性会触发Window_OnClosing事件privatevoidBtnCancel_Click(objectsender,RoutedEventArgse){......
  • WPF数据绑定UI不更新原因之一
    下面是有问题的代码privateObservableCollection<Params>_values;publicMainWindow(){InitializeComponent();this.dgrid1.ItemsSource=_values;}privatevoidbtnUploadClick(objectsender,RoutedEventArgse){_values=newObservableCollection&......