沁源鸿远 发表于 2024-1-29 18:14:19

5.WPF样式Style

样式的类型叫Style,它继承于DispatcherObject,它最重要的几个属性如下:
TargetType属性:这是一个类类型,也就是一个反射,这个属性指明了当前样式要作用于哪种类型的控件上。因为WPF中有许多的控件,我们定义一个样式时,必须要指明这个样式的“适用范围”。
BasedOn属性:样式也有继承的概念,所以,BasedOn指明了当前样式继承于哪个样式
Triggers属性:这是一个集合属性,表示触发器的定义,当满足某些条件时,触发哪些行为,以使控件达到一定的“节目效果”。比如当鼠标移上去时,控件的背景颜色变成红色。这些的效果就可以通过定义控件的触发器来设置。
Setters属性:这也是一个集合属性,样式是控件属性的值的“提前设置”,所以,我们对控件属性的值的设置都是以Setter条目的形式,一条一条的放到这个Setters集合中。
Resources属性:这个属性叫资源字典。在正式讲解样式之前,我们要先介绍一下资源字典的概念及其用途。它表示一些资源,以字典的形式进行维护,方便程序引用。
 
在FrameworkElement类就有一个Style属性。而所有的控件都继承于FrameworkElement基类,所以,我们只需要将定义好的样式赋值到控件的Style属性即可
在Application.Resources中定义样式
<Application.Resources>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid><Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid></Application.Resources>在XAML中引用样式
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid>在引用样式时,我们有两种方式,分别是DynamicResource和StaticResource,后面再写上样式的名称。DynamicResource表示动态资源,StaticResource表示静态资源。这两者的区别是:静态资源在第一次编译后即确定其对象或值,之后不能对其进行修改。动态资源则是在运行时决定,当运行过程中真正需要时,才到资源目标中查找其值。因此,我们可以动态地修改它。由于动态资源的运行时才能确定其值,因此效率比静态资源要低。
 
资源字典与样式的用法
在项目中新建一个Style文件夹,右键-添加-资源字典文件。创建一个Button.xaml的资源文件,并在其中写下内容
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid>资源文件都是以ResourceDictionary实例开头的对象,然后我们在其中编写了一个Style样式
回到项目的App.xaml文件中,编写如下内容
<Application.Resources>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid><Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid></Application.Resources><Application.Resources>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid><Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid></Application.Resources><Application.Resources>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid><Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid></Application.Resources><Application.Resources>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid><Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid></Application.Resources><Application.Resources>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid><Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid></Application.Resources><Application.Resources>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid><Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid></Application.Resources><Application.Resources>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid><Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid></Application.Resources><Application.Resources>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid><Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid></Application.Resources><Application.Resources>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid><Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid></Application.Resources><Application.Resources>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid><Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid></Application.Resources><Application.Resources>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid><Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid></Application.Resources><Application.Resources>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid><Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid></Application.Resources><Application.Resources>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid><Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid></Application.Resources>我们在Application的Resources属性中实例化了一个ResourceDictionary对象,并在其中定义了两个SolidColorBrush对象,一个style样式,以及在MergedDictionaries集合中添加了一个ResourceDictionary对象,其数据源指向了Button.xaml资源文件。
最后,我们来到主窗体的前端代码:
<Application.Resources>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid><Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid></Application.Resources><Application.Resources>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid><Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid></Application.Resources><Application.Resources>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid><Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid></Application.Resources><Application.Resources>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid><Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid></Application.Resources><Application.Resources>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid><Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid></Application.Resources><Application.Resources>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid><Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid></Application.Resources><Application.Resources>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid><Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid></Application.Resources><Application.Resources>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid><Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid>
<Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid></Application.Resources><Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid><Grid>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
</ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
</Grid>  
Resources属性的值只能是一个ResourceDictionary对象,一个ResourceDictionary对象中可以定义多个资源。如果有多个ResourceDictionary对象,则必须使用MergedDictionaries属性。任意类型都可以在Resources中被实例化,但是必须在实例化时指明一个key,因为在xaml中要引入定义好的资源,都是以key进行查找的。
 

来源:https://www.cnblogs.com/MingQiu/p/17994791
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 5.WPF样式Style