翼度科技»论坛 编程开发 .net 查看内容

5.WPF样式Style

7

主题

7

帖子

21

积分

新手上路

Rank: 1

积分
21
样式的类型叫Style,它继承于DispatcherObject,它最重要的几个属性如下:
TargetType属性:这是一个类类型,也就是一个反射,这个属性指明了当前样式要作用于哪种类型的控件上。因为WPF中有许多的控件,我们定义一个样式时,必须要指明这个样式的“适用范围”。
BasedOn属性:样式也有继承的概念,所以,BasedOn指明了当前样式继承于哪个样式
Triggers属性:这是一个集合属性,表示触发器的定义,当满足某些条件时,触发哪些行为,以使控件达到一定的“节目效果”。比如当鼠标移上去时,控件的背景颜色变成红色。这些的效果就可以通过定义控件的触发器来设置。
Setters属性:这也是一个集合属性,样式是控件属性的值的“提前设置”,所以,我们对控件属性的值的设置都是以Setter条目的形式,一条一条的放到这个Setters集合中。
Resources属性:这个属性叫资源字典。在正式讲解样式之前,我们要先介绍一下资源字典的概念及其用途。它表示一些资源,以字典的形式进行维护,方便程序引用。
 
在FrameworkElement类就有一个Style属性。而所有的控件都继承于FrameworkElement基类,所以,我们只需要将定义好的样式赋值到控件的Style属性即可
在Application.Resources中定义样式
  1. <Application.Resources>
  2. <Grid>
  3. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  5.    
  6. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  7. </Grid><Grid>
  8. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  9.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  10.    
  11. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  12. </Grid>
  13. <Grid>
  14. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  15.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  16.    
  17. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  18. </Grid></Application.Resources>
复制代码
在XAML中引用样式
  1. <Grid>
  2. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  4.    
  5. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  6. </Grid>
复制代码
在引用样式时,我们有两种方式,分别是DynamicResource和StaticResource,后面再写上样式的名称。DynamicResource表示动态资源,StaticResource表示静态资源。这两者的区别是:静态资源在第一次编译后即确定其对象或值,之后不能对其进行修改。动态资源则是在运行时决定,当运行过程中真正需要时,才到资源目标中查找其值。因此,我们可以动态地修改它。由于动态资源的运行时才能确定其值,因此效率比静态资源要低。
 
资源字典与样式的用法
在项目中新建一个Style文件夹,右键-添加-资源字典文件。创建一个Button.xaml的资源文件,并在其中写下内容
  1. <Grid>
  2. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  4.    
  5. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  6. </Grid>
复制代码
资源文件都是以ResourceDictionary实例开头的对象,然后我们在其中编写了一个Style样式
回到项目的App.xaml文件中,编写如下内容
  1. <Application.Resources>
  2. <Grid>
  3. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  5.    
  6. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  7. </Grid><Grid>
  8. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  9.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  10.    
  11. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  12. </Grid>
  13. <Grid>
  14. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  15.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  16.    
  17. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  18. </Grid></Application.Resources><Application.Resources>
  19. <Grid>
  20. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  21.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  22.    
  23. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  24. </Grid><Grid>
  25. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  26.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  27.    
  28. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  29. </Grid>
  30. <Grid>
  31. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  32.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  33.    
  34. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  35. </Grid></Application.Resources><Application.Resources>
  36. <Grid>
  37. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  38.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  39.    
  40. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  41. </Grid><Grid>
  42. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  43.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  44.    
  45. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  46. </Grid>
  47. <Grid>
  48. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  49.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  50.    
  51. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  52. </Grid></Application.Resources><Application.Resources>
  53. <Grid>
  54. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  55.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  56.    
  57. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  58. </Grid><Grid>
  59. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  60.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  61.    
  62. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  63. </Grid>
  64. <Grid>
  65. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  66.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  67.    
  68. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  69. </Grid></Application.Resources><Application.Resources>
  70. <Grid>
  71. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  72.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  73.    
  74. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  75. </Grid><Grid>
  76. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  77.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  78.    
  79. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  80. </Grid>
  81. <Grid>
  82. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  83.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  84.    
  85. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  86. </Grid></Application.Resources><Application.Resources>
  87. <Grid>
  88. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  89.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  90.    
  91. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  92. </Grid><Grid>
  93. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  94.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  95.    
  96. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  97. </Grid>
  98. <Grid>
  99. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  100.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  101.    
  102. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  103. </Grid></Application.Resources><Application.Resources>
  104. <Grid>
  105. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  106.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  107.    
  108. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  109. </Grid><Grid>
  110. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  111.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  112.    
  113. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  114. </Grid>
  115. <Grid>
  116. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  117.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  118.    
  119. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  120. </Grid></Application.Resources><Application.Resources>
  121. <Grid>
  122. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  123.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  124.    
  125. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  126. </Grid><Grid>
  127. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  128.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  129.    
  130. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  131. </Grid>
  132. <Grid>
  133. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  134.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  135.    
  136. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  137. </Grid></Application.Resources><Application.Resources>
  138. <Grid>
  139. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  140.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  141.    
  142. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  143. </Grid><Grid>
  144. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  145.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  146.    
  147. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  148. </Grid>
  149. <Grid>
  150. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  151.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  152.    
  153. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  154. </Grid></Application.Resources><Application.Resources>
  155. <Grid>
  156. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  157.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  158.    
  159. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  160. </Grid><Grid>
  161. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  162.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  163.    
  164. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  165. </Grid>
  166. <Grid>
  167. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  168.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  169.    
  170. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  171. </Grid></Application.Resources><Application.Resources>
  172. <Grid>
  173. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  174.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  175.    
  176. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  177. </Grid><Grid>
  178. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  179.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  180.    
  181. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  182. </Grid>
  183. <Grid>
  184. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  185.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  186.    
  187. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  188. </Grid></Application.Resources><Application.Resources>
  189. <Grid>
  190. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  191.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  192.    
  193. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  194. </Grid><Grid>
  195. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  196.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  197.    
  198. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  199. </Grid>
  200. <Grid>
  201. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  202.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  203.    
  204. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  205. </Grid></Application.Resources><Application.Resources>
  206. <Grid>
  207. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  208.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  209.    
  210. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  211. </Grid><Grid>
  212. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  213.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  214.    
  215. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  216. </Grid>
  217. <Grid>
  218. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  219.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  220.    
  221. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  222. </Grid></Application.Resources>  
复制代码
我们在Application的Resources属性中实例化了一个ResourceDictionary对象,并在其中定义了两个SolidColorBrush对象,一个style样式,以及在MergedDictionaries集合中添加了一个ResourceDictionary对象,其数据源指向了Button.xaml资源文件。
最后,我们来到主窗体的前端代码:
  1. <Application.Resources>
  2. <Grid>
  3. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  5.    
  6. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  7. </Grid><Grid>
  8. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  9.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  10.    
  11. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  12. </Grid>
  13. <Grid>
  14. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  15.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  16.    
  17. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  18. </Grid></Application.Resources><Application.Resources>
  19. <Grid>
  20. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  21.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  22.    
  23. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  24. </Grid><Grid>
  25. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  26.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  27.    
  28. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  29. </Grid>
  30. <Grid>
  31. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  32.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  33.    
  34. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  35. </Grid></Application.Resources><Application.Resources>
  36. <Grid>
  37. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  38.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  39.    
  40. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  41. </Grid><Grid>
  42. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  43.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  44.    
  45. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  46. </Grid>
  47. <Grid>
  48. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  49.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  50.    
  51. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  52. </Grid></Application.Resources><Application.Resources>
  53. <Grid>
  54. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  55.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  56.    
  57. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  58. </Grid><Grid>
  59. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  60.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  61.    
  62. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  63. </Grid>
  64. <Grid>
  65. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  66.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  67.    
  68. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  69. </Grid></Application.Resources><Application.Resources>
  70. <Grid>
  71. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  72.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  73.    
  74. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  75. </Grid><Grid>
  76. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  77.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  78.    
  79. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  80. </Grid>
  81. <Grid>
  82. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  83.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  84.    
  85. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  86. </Grid></Application.Resources><Application.Resources>
  87. <Grid>
  88. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  89.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  90.    
  91. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  92. </Grid><Grid>
  93. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  94.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  95.    
  96. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  97. </Grid>
  98. <Grid>
  99. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  100.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  101.    
  102. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  103. </Grid></Application.Resources><Application.Resources>
  104. <Grid>
  105. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  106.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  107.    
  108. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  109. </Grid><Grid>
  110. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  111.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  112.    
  113. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  114. </Grid>
  115. <Grid>
  116. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  117.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  118.    
  119. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  120. </Grid></Application.Resources><Application.Resources>
  121. <Grid>
  122. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  123.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  124.    
  125. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  126. </Grid><Grid>
  127. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  128.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  129.    
  130. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  131. </Grid>
  132. <Grid>
  133. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  134.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  135.    
  136. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  137. </Grid></Application.Resources><Grid>
  138. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  139.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  140.    
  141. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  142. </Grid><Grid>
  143. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  144.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  145.    
  146. </ResourceDictionary><Button Content="文字块" Style="{StaticResource ButtonStyle}"/>
  147. </Grid>
复制代码
 
Resources属性的值只能是一个ResourceDictionary对象,一个ResourceDictionary对象中可以定义多个资源。如果有多个ResourceDictionary对象,则必须使用MergedDictionaries属性。任意类型都可以在Resources中被实例化,但是必须在实例化时指明一个key,因为在xaml中要引入定义好的资源,都是以key进行查找的。
 

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

举报 回复 使用道具