5.WPF样式Style

  • 5.WPF样式Style已关闭评论
  • 59 次浏览
  • A+
所属分类:.NET技术
摘要

样式的类型叫Style,它继承于DispatcherObject,它最重要的几个属性如下:TargetType属性:这是一个类类型,也就是一个反射,这个属性指明了当前样式要作用于哪种类型的控件上。因为WPF中有许多的控件,我们定义一个样式时,必须要指明这个样式的“适用范围”。

样式的类型叫Style,它继承于DispatcherObject,它最重要的几个属性如下:

TargetType属性:这是一个类类型,也就是一个反射,这个属性指明了当前样式要作用于哪种类型的控件上。因为WPF中有许多的控件,我们定义一个样式时,必须要指明这个样式的“适用范围”。

BasedOn属性:样式也有继承的概念,所以,BasedOn指明了当前样式继承于哪个样式

Triggers属性:这是一个集合属性,表示触发器的定义,当满足某些条件时,触发哪些行为,以使控件达到一定的“节目效果”。比如当鼠标移上去时,控件的背景颜色变成红色。这些的效果就可以通过定义控件的触发器来设置。

Setters属性:这也是一个集合属性,样式是控件属性的值的“提前设置”,所以,我们对控件属性的值的设置都是以Setter条目的形式,一条一条的放到这个Setters集合中。

Resources属性:这个属性叫资源字典。在正式讲解样式之前,我们要先介绍一下资源字典的概念及其用途。它表示一些资源,以字典的形式进行维护,方便程序引用。

 

在FrameworkElement类就有一个Style属性。而所有的控件都继承于FrameworkElement基类,所以,我们只需要将定义好的样式赋值到控件的Style属性即可

在Application.Resources中定义样式

<Application.Resources>         <Style x:Key="ButtonStyle" TargetType="Button">             <Setter Property="Width" Value="100"/>             <Setter Property="Height" Value="30"/>             <Setter Property="Background" Value="Red"/>             <Setter Property="Foreground" Value="White"/>         </Style>     </Application.Resources>

在XAML中引用样式

<Grid>     <Button Content="文字块" Style="{StaticResource ButtonStyle}"/> </Grid>

在引用样式时,我们有两种方式,分别是DynamicResource和StaticResource,后面再写上样式的名称。DynamicResource表示动态资源,StaticResource表示静态资源。这两者的区别是:静态资源在第一次编译后即确定其对象或值,之后不能对其进行修改。动态资源则是在运行时决定,当运行过程中真正需要时,才到资源目标中查找其值。因此,我们可以动态地修改它。由于动态资源的运行时才能确定其值,因此效率比静态资源要低。

 

资源字典与样式的用法

在项目中新建一个Style文件夹,右键-添加-资源字典文件。创建一个Button.xaml的资源文件,并在其中写下内容

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">     <Style x:Key="BlueButtonStyle" TargetType="Button">         <Setter Property="Width" Value="100"/>         <Setter Property="Height" Value="30"/>         <Setter Property="Background" Value="Blue"/>         <Setter Property="Foreground" Value="White"/>         <Setter Property="Margin" Value="3"/>     </Style> </ResourceDictionary>

资源文件都是以ResourceDictionary实例开头的对象,然后我们在其中编写了一个Style样式

回到项目的App.xaml文件中,编写如下内容

<Application x:Class="HelloWorld.App"              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"              xmlns:local="clr-namespace:HelloWorld"              StartupUri="MainWindow.xaml">     <Application.Resources>         <ResourceDictionary>                          <SolidColorBrush x:Key="ButtonBackground" Color="Red"/>             <SolidColorBrush x:Key="ButtonForeground" Color="White"/>                          <Style x:Key="ButtonStyle" TargetType="Button">                 <Setter Property="Width" Value="100"/>                 <Setter Property="Height" Value="30"/>                 <Setter Property="Background" Value="{StaticResource ButtonBackground}"/>                 <Setter Property="Foreground" Value="{StaticResource ButtonForeground}"/>                 <Setter Property="Margin" Value="3"/>             </Style>                          <ResourceDictionary.MergedDictionaries>                 <ResourceDictionary Source="Style/Button.xaml"/>             </ResourceDictionary.MergedDictionaries>                      </ResourceDictionary>               </Application.Resources> </Application>

我们在Application的Resources属性中实例化了一个ResourceDictionary对象,并在其中定义了两个SolidColorBrush对象,一个style样式,以及在MergedDictionaries集合中添加了一个ResourceDictionary对象,其数据源指向了Button.xaml资源文件。

最后,我们来到主窗体的前端代码:

<Window x:Class="HelloWorld.MainWindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"         xmlns:local="clr-namespace:HelloWorld"          xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"         mc:Ignorable="d" FontSize="14"         Title="WPF中文网之控件课程 - www.wpfsoft.com" Height="350" Width="500">     <Window.Resources>         <Style x:Key="GreenButtonStyle" TargetType="Button">             <Setter Property="Width" Value="100"/>             <Setter Property="Height" Value="30"/>             <Setter Property="Background" Value="Green"/>             <Setter Property="Foreground" Value="White"/>             <Setter Property="Margin" Value="3"/>         </Style>     </Window.Resources>     <StackPanel VerticalAlignment="Center">         <Button Content="红色按钮" Style="{StaticResource ButtonStyle}"/>         <Button Content="蓝色按钮" Style="{StaticResource BlueButtonStyle}"/>         <Button Content="绿色按钮" Style="{StaticResource GreenButtonStyle}"/>         <Button Content="橙色按钮">             <Button.Style>                 <Style TargetType="Button">                     <Setter Property="Width" Value="100"/>                     <Setter Property="Height" Value="30"/>                     <Setter Property="Background" Value="Orange"/>                     <Setter Property="Foreground" Value="White"/>                     <Setter Property="Margin" Value="3"/>                 </Style>             </Button.Style>         </Button>      </StackPanel> </Window>

 

Resources属性的值只能是一个ResourceDictionary对象,一个ResourceDictionary对象中可以定义多个资源。如果有多个ResourceDictionary对象,则必须使用MergedDictionaries属性。任意类型都可以在Resources中被实例化,但是必须在实例化时指明一个key,因为在xaml中要引入定义好的资源,都是以key进行查找的。