WPF系列:样式

  一般简单的样式我们可以直接写在控件中

<Button Canvas.Left="20" Canvas.Top="30" Width="100" Height="120" Content="A"/>

  也可以通过Style属性定义该系列控件的样式

  <Style TargetType="{x:Type Button}">
      <Setter Property="Background" Value="Green"></Setter>
      <Setter Property="FontSize" Value="24" ></Setter>
  </Style>

  然后通过键值,可以赋予该系列某一类型特定的样式,这个有点像CSS里的class

   <Style x:Key="ButtonMenu">
       <Setter Property="Button.Background" Value="Red"></Setter>
       <Setter Property="Button.Width" Value="100"></Setter>
       <Setter Property="Button.Height" Value="100"></Setter>
       <Setter Property="Button.FontSize" Value="24" ></Setter>
   </Style>

  WPF中的样式还可以通过BasedOn属性继承样式

 <Style x:Key="ButtonTitle" BasedOn="{StaticResource ButtonMenu}" TargetType="Button">
     <Setter Property="Background">
         <Setter.Value>
             <LinearGradientBrush>
                 <GradientStop Offset="0.2" Color="LightBlue"></GradientStop>
                 <GradientStop Offset="0.5" Color="Blue"></GradientStop>
                 <GradientStop Offset="0.9" Color="DarkBlue"></GradientStop>
             </LinearGradientBrush>
         </Setter.Value>
     </Setter>
     <Setter Property="Button.Width" Value="100"></Setter>
     <Setter Property="Button.Height" Value="100"></Setter>
 </Style>

  最后把所有代码合在一起

    <Canvas>
        <Canvas.Resources>
            <Style TargetType="{x:Type Button}">
                <Setter Property="Background" Value="Green"></Setter>
                <Setter Property="FontSize" Value="24" ></Setter>
            </Style>
            <Style x:Key="ButtonMenu">
                <Setter Property="Button.Background" Value="Red"></Setter>
                <Setter Property="Button.Width" Value="100"></Setter>
                <Setter Property="Button.Height" Value="100"></Setter>
                <Setter Property="Button.FontSize" Value="24" ></Setter>
            </Style>
            <Style x:Key="ButtonTitle" BasedOn="{StaticResource ButtonMenu}" TargetType="Button">
                <Setter Property="Background">
                    <Setter.Value>
                        <LinearGradientBrush>
                            <GradientStop Offset="0.2" Color="LightBlue"></GradientStop>
                            <GradientStop Offset="0.5" Color="Blue"></GradientStop>
                            <GradientStop Offset="0.9" Color="DarkBlue"></GradientStop>
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
            </Style>
        </Canvas.Resources>
        <Button Canvas.Left="20" Canvas.Top="30" Width="100" Height="120" Content="A"/>
        <Button Canvas.Left="150" Canvas.Top="30" Style="{StaticResource ButtonMenu}" Content="B" />
        <Button Canvas.Left="280" Canvas.Top="30" Style="{StaticResource ButtonTitle}" Content="C" />
    </Canvas>

WPF系列:样式

上一篇:php-SMTP错误:无法连接到服务器:权限被拒绝(13)


下一篇:Swift 之Protocol在cocoa中的使用范例搜集(一)