①在前面的几节讲解中,每个按钮都是通过设置Template或Style属性将自身关联到适当模板的。但如果想让应用程序中的每个按钮自动应用模板,按照上面的方式进行设置就显得有点麻烦了。
②为了解决上述问题,可以通过类型样式,这样样式会自动影响相应的元素类型并设置Template属性。下面是一个样式示例,应将该样式放到您的资源集合中,从而为按钮提供新外观。
<Style TargetType="{x:Type Button}">
<Setter Property="Control.Template" Value="{StaticResource GradientButtonTemplate}"></Setter>
</Style>
注意:此时的样式不要指定键名,这意味着将该样式用到所有Button类型元素。③可通过设置元素的Style属性为null值,退出样式。
<Button Style="{x:Null}" ... />
④我们将基于类型的样式的资源字典称为主题。通过主题我们可为已有应用程序的所有控件重新应用皮肤,而根本不需要更改用户界面标记,只需要将主题合并到App.xaml文件的Application.Resources集合中。例如:
<Application x:Class="ControlTemplates.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Menu.xaml">
<Application.Resources>
<ResourceDictionary Source="ExpressionDark.xaml"/>
</Application.Resources>
</Application>