WPF学习笔记<一>

一.Named Style命名(指定)样式?

(1)为用户界面的控件或一系列控件指定样式,而不是采用默认缺省的样式。 先设计style,明确其key。再引用key。

(2)源代码

二. Typed style 类型化样式?

(1)为一类控件指定相同的样式. 先设计style,明确其TargetType.再引用TargetType.

(2)源代码

隐藏行号 复制代码 Named Style and Typed Style
  1. <Window x:Class="WpfApplication1.Window1"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         Title="Window with style" Height="286" Width="510">
  5.     <Window.Resources>
  6.         
  7.         <!--BrushConverter resource-->
  8.         <LinearGradientBrush x:Key="NormalBrush" EndPoint="0,1" StartPoint="0,0">
  9.             <GradientStop Color="Orange" Offset="0.0" />
  10.             <GradientStop Color="White" Offset="0.5" />
  11.             <GradientStop Color="Yellow" Offset="1.0" />            
  12.         </LinearGradientBrush>
  13.        
  14.         <LinearGradientBrush x:Key="NormalBorderBrush" EndPoint="0,1" StartPoint="0,0">
  15.             <GradientStop Color="Blue" Offset="0.0" />
  16.             <GradientStop Color="Black" Offset="1.0" />
  17.         </LinearGradientBrush>
  18.         <!--named style--> 
  19.         <!--chapter6.1-->
  20.       <Style x:Key="MyStyle">
  21.             <Setter Property="Control.FontWeight" Value="bold" />
  22.             <Setter Property="Control.Background" Value="{DynamicResource NormalBrush}" />
  23.             <Setter Property="Control.BorderBrush" Value="{DynamicResource NormalBorderBrush}" />
  24.             <Setter Property="Control.Width" Value="88" />
  25.             <Setter Property="Control.Height" Value="24" />
  26.             <Setter Property="Control.Margin" Value="4" />
  27.         </Style>
  28.         <!--chapter6.2 with  the custom style automatically applied to both instances of the button contro-->
  29.         <!--typed style-->
  30.         <Style TargetType="{x:Type Button}">
  31.             <Setter Property="Background" Value="{StaticResource NormalBrush}" />
  32.             <Setter Property="BorderBrush" Value="{StaticResource NormalBorderBrush}" />
  33.             <Setter Property="Width" Value="40" />
  34.             <Setter Property="Height" Value="24" />
  35.             <Setter Property="Margin" Value="4" />
  36.             <Setter Property="Foreground" Value="green" />
  37.         </Style>
  38.     </Window.Resources>
  39.     <Grid>
  40.         <StackPanel Name="stackPanel1" Orientation="Vertical" >
  41.             <Button Style="{StaticResource MyStyle}" Content="named style"/>
  42.             <Button Style="{StaticResource MyStyle}" Content="named style"/>
  43.             <Button Content="Default Style" Height="24"  Width="88"  Margin="4"/>         
  44.             <StackPanel Name="stackPanel2" Orientation="Horizontal" >
  45.                 <Button>One</Button>
  46.                 <Button>Two</Button>
  47.                 <Button>Three  </Button>
  48.             </StackPanel>
  49.         </StackPanel>
  50.     </Grid>
  51. </Window>

(3)运行结果

1

三. override style properties 重载样式

(1)Styles set the initial appearance of a control,but you can override any of the values they set in the inline XAML for any element.The control will automatically use these instead of taking the values from a named or typed style.

(2)源代码

隐藏行号 复制代码 Override Style
  1. <Window x:Class="WpfApplication1.Window2"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         Title="Window2" Height="300" Width="300">
  5.     <Window.Resources>
  6.         <!--Typed Style-->
  7.         <Style TargetType="{x:Type Button}">
  8.             <Setter Property="Margin" Value="4"/>
  9.             <Setter Property="Width" Value="80"/>
  10.             <Setter Property="Height" Value="24"/>
  11.             <Setter Property="FontWeight" Value="Bold"/>
  12.         </Style>
  13.     </Window.Resources>
  14.     <Grid Margin="20">
  15.         <StackPanel Orientation="Horizontal">
  16.             <Button>One</Button>
  17.             <!--Override the fontweight in the style-->
  18.             <Button FontWeight="Thin">Two</Button>
  19.         </StackPanel>
  20.     </Grid>
  21. </Window>

(3)运行结果

2

四.inherit from a common base style 继承样式

(1)源代码

隐藏行号 复制代码 Override and Inherit Style
  1. <Window x:Class="WpfApplication1.Window3"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         Title="Window3" Height="278" Width="767">
  5.     <Window.Resources>
  6.         <!--Base Style-->
  7.         <Style x:Key="BaseControlStyle" TargetType="{x:Type Control}">
  8.             <Setter Property="FontFamily" Value="Tahoma"/>
  9.             <Setter Property="FontSize" Value="14pt"/>
  10.             <Setter Property="FontStyle" Value="Italic"/>
  11.         </Style>
  12.         <!--Button Style-->
  13.         <Style TargetType="{x:Type Button}" BasedOn="{StaticResource BaseControlStyle}">
  14.             <!--Add any overriding property values here-->
  15.             <Setter Property="FontWeight" Value="Bold"/>
  16.         </Style>
  17.         <!--CheckBox Style-->
  18.         <Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource BaseControlStyle}">
  19.         </Style>
  20.         <!--TextBox Style-->
  21.         <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource BaseControlStyle}">
  22.         </Style>
  23.     </Window.Resources>
  24.     <Grid>
  25.         <StackPanel Margin="73,0,92,0">
  26.             <CheckBox>CheckBox with inherited style</CheckBox>
  27.             <TextBox >TextBox  with inherited style</TextBox>
  28.             <Button>Button with inherited style</Button>
  29.             <Button FontWeight="Light" >Button with overridden style</Button>
  30.             <TextBlock >TextBlock with overridden style</TextBlock>
  31.             <ComboBox >ComboBox with overridden style</ComboBox>
  32.         </StackPanel>
  33.     </Grid>
  34. </Window>

(2)运行结果

3

参考资料(感谢作者)

1 WPF Recipes in C#2008 Author SAM NOBLE ,SAM BOURTON AND ALLEN JONES

2 http://www.cnblogs.com/czhenq/archive/2006/11/27/574423.html

原文地址:https://www.cnblogs.com/gisalameda/p/1870126.html