WPF样式

样式(styles)是组织和重用格式化选项的重要工具。WPF样式与CSS类似,通过WPF样式可以定义通用的格式化特性集合,并且为了保证一致性,在整个应用程序中应用它们。

一、style的定义位置

window窗口中的Style的例子:

<Window>
   <Window.Resources>
        <Style x:Key="TestStyle" TargetType="{x:Type Button}">
           <Setter Property="Background" Value="MediumPurple"></Setter>
        </Style>
    </Window.Resources>
   <Grid>
     <Button Style="{StaticResource TestStyle}" Name="button2" Height="23" Width="56">
     </Button>
   </Grid>
</Window>

样式的名称:通过x:Key="TestStyle"来指定。

在Grid中定义Style的方式也类似

<Window>
  <Grid>
        <Grid.Resources>
            <Style x:Key="GridStyle" TargetType="{x:Type Button}">
                <Setter Property="Background" Value="RoyalBlue"></Setter>
            </Style>
        </Grid.Resources>
        <Button Style="{StaticResource GridStyle}" Name="button2" Height="23" Width="56">     </Button>   </Grid>
</Window>

style的使用优先级

在WPF中,Style是基于Dependency Property的.Dependency Property设计的精髓在于把字段的存取和对象(Dependency Object)剥离开,一个属性值内部用多个字段来存储,根据取值条件的优先级来决定当前属性应该取哪个字段。

Dependency Property取值条件的优先级是(从上到下优先级从低到高):

1: public enum BaseValueSource
   2: {
   3:     Unknown,
   4:     Default,
   5:     Inherited,
   6:     DefaultStyle,
   7:     DefaultStyleTrigger,
   8:     Style,
   9:     TemplateTrigger,
  10:     StyleTrigger,
  11:     ImplicitStyleReference,
  12:     ParentTemplate,
  13:     ParentTemplateTrigger,
  14:     Local
  15: }

对于一个具体例子来说:

1: <Window>
   2:     <Window.Resources>
   3:         <Style TargetType="{x:Type Button}" x:Key="ButtonStyle">
   4:             <Setter Property="Width" Value="60"/>
   5:             <Style.Triggers>
   6:                 <Trigger Property="IsMouseOver" Value="True">
   7:                     <Setter Property="Width" Value="80"/>
   8:                 </Trigger>
   9:             </Style.Triggers>
  10:         </Style>
  11:     </Window.Resources>
  12:     <Grid>
  13:         <Button x:Name="button1" Style="{StaticResource ButtonStyle}" Background="{DynamicResource brush}" Width="20"/>
  14:     </Grid>
  15: </Window>

第4行用Style的Setter设置Width=60,这个优先级是Style;第6行当IsMouseOver为True时设置Width=80,这个优先级是StyleTrigger;第13行使用Style的Button定义Width=20,这个优先级是Local。Local具有最高的优先级,所以即使鼠标移到Button上,第6行的Trigger也会因为优先级不够高而不起作用。如果去掉了第13行中的Width=20,那么鼠标移到Button上时Width会变为80,鼠标移开后会回到第4行的设置的60来。

顺便提一下:

在WPF中,资源的执行查找优先级最高为local,最低为系统资源.

image

资源字典中定义style

定义:

Dictionary2.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="DiStyle" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Red"></Setter>       
    </Style>
</ResourceDictionary>

如果要想在程序中使用资源字典中的样式或者其他资源,那么必须在APP.xaml(应用程序中资源中)使用合并资源字典,加载资源字典。

这是由于资源字典Dictionary2.xaml是独立的xaml文件,它所处的位置和window(当然也可以是page)类似,因此只能将它加入到App.xaml文件中,让App.xaml识别它,如此整个程序才能正确引用资源字典中的资源。

设置,如下所示:

App.xaml:

<Application x:Class="TestWPFWindow.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
         <ResourceDictionary>
             <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary2.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
         </ResourceDictionary>
    </Application.Resources>
</Application>

使用,和其他定义类似

<Window x:Class="TestWPFWindow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>  
        <Button Style="{StaticResource DiStyle}" Content="Button" Height="23" HorizontalAlignment="Left" Margin="54,44,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>

参考:http://www.cnblogs.com/Zhouyongh/archive/2011/08/01/2123610.html

原文地址:https://www.cnblogs.com/aoguren/p/4216837.html