一.Named Style命名(指定)样式?
(1)为用户界面的控件或一系列控件指定样式,而不是采用默认缺省的样式。 先设计style,明确其key。再引用key。
(2)源代码
二. Typed style 类型化样式?
(1)为一类控件指定相同的样式. 先设计style,明确其TargetType.再引用TargetType.
(2)源代码
-
<Window x:Class="WpfApplication1.Window1"
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-
Title="Window with style" Height="286" Width="510">
-
<Window.Resources>
-
-
<!--BrushConverter resource-->
-
<LinearGradientBrush x:Key="NormalBrush" EndPoint="0,1" StartPoint="0,0">
-
<GradientStop Color="Orange" Offset="0.0" />
-
<GradientStop Color="White" Offset="0.5" />
-
<GradientStop Color="Yellow" Offset="1.0" />
-
</LinearGradientBrush>
-
-
<LinearGradientBrush x:Key="NormalBorderBrush" EndPoint="0,1" StartPoint="0,0">
-
<GradientStop Color="Blue" Offset="0.0" />
-
<GradientStop Color="Black" Offset="1.0" />
-
</LinearGradientBrush>
-
-
<!--named style-->
-
<!--chapter6.1-->
-
<Style x:Key="MyStyle">
-
<Setter Property="Control.FontWeight" Value="bold" />
-
<Setter Property="Control.Background" Value="{DynamicResource NormalBrush}" />
-
<Setter Property="Control.BorderBrush" Value="{DynamicResource NormalBorderBrush}" />
-
<Setter Property="Control.Width" Value="88" />
-
<Setter Property="Control.Height" Value="24" />
-
<Setter Property="Control.Margin" Value="4" />
-
</Style>
-
<!--chapter6.2 with the custom style automatically applied to both instances of the button contro-->
-
<!--typed style-->
-
<Style TargetType="{x:Type Button}">
-
<Setter Property="Background" Value="{StaticResource NormalBrush}" />
-
<Setter Property="BorderBrush" Value="{StaticResource NormalBorderBrush}" />
-
<Setter Property="Width" Value="40" />
-
<Setter Property="Height" Value="24" />
-
<Setter Property="Margin" Value="4" />
-
<Setter Property="Foreground" Value="green" />
-
</Style>
-
</Window.Resources>
-
<Grid>
-
<StackPanel Name="stackPanel1" Orientation="Vertical" >
-
<Button Style="{StaticResource MyStyle}" Content="named style"/>
-
<Button Style="{StaticResource MyStyle}" Content="named style"/>
-
<Button Content="Default Style" Height="24" Width="88" Margin="4"/>
-
<StackPanel Name="stackPanel2" Orientation="Horizontal" >
-
<Button>One</Button>
-
<Button>Two</Button>
-
<Button>Three </Button>
-
</StackPanel>
-
</StackPanel>
-
</Grid>
-
</Window>
-
(3)运行结果
三. 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)源代码
-
<Window x:Class="WpfApplication1.Window2"
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-
Title="Window2" Height="300" Width="300">
-
<Window.Resources>
-
<!--Typed Style-->
-
<Style TargetType="{x:Type Button}">
-
<Setter Property="Margin" Value="4"/>
-
<Setter Property="Width" Value="80"/>
-
<Setter Property="Height" Value="24"/>
-
<Setter Property="FontWeight" Value="Bold"/>
-
</Style>
-
</Window.Resources>
-
<Grid Margin="20">
-
<StackPanel Orientation="Horizontal">
-
<Button>One</Button>
-
<!--Override the fontweight in the style-->
-
<Button FontWeight="Thin">Two</Button>
-
</StackPanel>
-
</Grid>
-
</Window>
-
(3)运行结果
四.inherit from a common base style 继承样式
(1)源代码
-
<Window x:Class="WpfApplication1.Window3"
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-
Title="Window3" Height="278" Width="767">
-
<Window.Resources>
-
<!--Base Style-->
-
<Style x:Key="BaseControlStyle" TargetType="{x:Type Control}">
-
<Setter Property="FontFamily" Value="Tahoma"/>
-
<Setter Property="FontSize" Value="14pt"/>
-
<Setter Property="FontStyle" Value="Italic"/>
-
</Style>
-
<!--Button Style-->
-
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource BaseControlStyle}">
-
<!--Add any overriding property values here-->
-
<Setter Property="FontWeight" Value="Bold"/>
-
</Style>
-
<!--CheckBox Style-->
-
<Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource BaseControlStyle}">
-
</Style>
-
<!--TextBox Style-->
-
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource BaseControlStyle}">
-
</Style>
-
</Window.Resources>
-
<Grid>
-
<StackPanel Margin="73,0,92,0">
-
<CheckBox>CheckBox with inherited style</CheckBox>
-
<TextBox >TextBox with inherited style</TextBox>
-
<Button>Button with inherited style</Button>
-
<Button FontWeight="Light" >Button with overridden style</Button>
-
<TextBlock >TextBlock with overridden style</TextBlock>
-
<ComboBox >ComboBox with overridden style</ComboBox>
-
</StackPanel>
-
</Grid>
-
</Window>
-
(2)运行结果
参考资料(感谢作者)
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