WFP 样式(复习用)

WPF的样式类似HTML的CSS,可以在设计页面或者字典文件中设计样式,然后在控件上引用样式。

一、样式的两类

基础样式 不需要用<Style> </Style>括起来。

   <Window.Resources>
        <FontFamily x:Key="ButtonFontFamily">Times New Roman</FontFamily>
        <sys:Double x:Key="ButtonFontSize">18</sys:Double>
        <FontWeight x:Key="ButtonFontWeight">Bold</FontWeight>
    </Window.Resources>

普通样式

<Window.Resources>
    <Style x:Key="BigFontButtonStyle"> //如果加上TargetType="TextBlock",这个样式只能用在TextBlock
    <Setter Property="Control.FontFamily" Value="Times New Roman"/> 
    <Setter Property="Control.FontSize" Value="18"/>
    <Setter Property="Control.FontWeight" Value="Bold"/>
  </Style>
</Window.Resources>

指定某类控件的样式。如下将设置页面中所有的TextBlock样式。 注意这里的<Style TargetType="TextBlock">中没有基础样式的x:Key="BigFontButtonStyle"

<Window.Resources>
    <Style  TargetType="TextBlock">
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="TextAlignment" Value="Center"/>
        <Setter Property="Padding" Value="5"/>    
    </Style>   
</Window.Resources>

二、样式的继承

可以在资源文件中定义一个基础样式,被其他样式继承(或者叫做扩展)。

<Window.Resources>
    <Style x:Key="BaseOnStyle" TargetType="TextBlock">
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="TextAlignment" Value="Center"/>
        <Setter Property="Padding" Value="5"/>   
    </Style>

    <Style x:Key="TextBlockStyle" TargetType="TextBlock"  BasedOn="{StaticResource BaseOnStyle}">
        <Setter Property="Control.Foreground" Value="Red"/>
    </Style>
</Window.Resources>

猜你喜欢

转载自www.cnblogs.com/KQNLL/p/9190089.html