参考自
http://www.cnblogs.com/SkyD/archive/2008/08/09/1264294.html
,非常感谢。
用Style的BaseOn属性可以实现WPF样式的显式继承,例如,有一个基样式:
用Style的BaseOn属性可以实现WPF样式的显式继承,例如,有一个基样式:
Code
<Style x:Key="BASE">
<Setter Property="Control.Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Offset="0" Color="Green"/>
<GradientStop Offset="0.5" Color="Blue"/>
<GradientStop Offset="1" Color="Red"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
下面分别定义2个样式继承它:
而且还可以继续继承上面定义的样式,例如对Button的样式再次扩展:
<Style x:Key="BASE">
<Setter Property="Control.Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Offset="0" Color="Green"/>
<GradientStop Offset="0.5" Color="Blue"/>
<GradientStop Offset="1" Color="Red"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
下面分别定义2个样式继承它:
Code
<Style BasedOn="{StaticResource BASE}" TargetType="{x:Type Button}">
<Setter Property="BorderBrush" Value="Red"></Setter>
</Style>
<Style BasedOn="{StaticResource BASE}" TargetType="{x:Type TextBox}">
<Setter Property="BorderBrush" Value="Black"></Setter>
</Style>
<Style BasedOn="{StaticResource BASE}" TargetType="{x:Type Button}">
<Setter Property="BorderBrush" Value="Red"></Setter>
</Style>
<Style BasedOn="{StaticResource BASE}" TargetType="{x:Type TextBox}">
<Setter Property="BorderBrush" Value="Black"></Setter>
</Style>
而且还可以继续继承上面定义的样式,例如对Button的样式再次扩展:
<
Style BasedOn
=
"
{StaticResource {x:Type Button}}
"
x:Key
=
"
NewButton
"
TargetType
=
"
{x:Type Button}
"
>
< Setter Property = " Margin " Value = " 10,0,0,0 " ></ Setter >
</ Style >
下面使用这些样式:
< Setter Property = " Margin " Value = " 10,0,0,0 " ></ Setter >
</ Style >
<
Grid
>
< StackPanel Width = " 250 " Height = " 400 " >
< Button Height = " 30 " ></ Button >
< TextBox Height = " 30 " ></ TextBox >
< Button Style = " {StaticResource NewButton} " Height = " 30 " />
</ StackPanel >
</ Grid >
完整的代码如下:
< StackPanel Width = " 250 " Height = " 400 " >
< Button Height = " 30 " ></ Button >
< TextBox Height = " 30 " ></ TextBox >
< Button Style = " {StaticResource NewButton} " Height = " 30 " />
</ StackPanel >
</ Grid >
Code
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<Style x:Key="BASE">
<Setter Property="Control.Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Offset="0" Color="Green"/>
<GradientStop Offset="0.5" Color="Blue"/>
<GradientStop Offset="1" Color="Red"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
<Style BasedOn="{StaticResource BASE}" TargetType="{x:Type Button}">
<Setter Property="BorderBrush" Value="Red"></Setter>
</Style>
<Style BasedOn="{StaticResource BASE}" TargetType="{x:Type TextBox}">
<Setter Property="BorderBrush" Value="Black"></Setter>
</Style>
<Style BasedOn="{StaticResource {x:Type Button}}" x:Key="NewButton" TargetType="{x:Type Button}">
<Setter Property="Margin" Value="10,0,0,0"></Setter>
</Style>
</Page.Resources>
<Grid>
<StackPanel Width="250" Height="400">
<Button Height="30"></Button>
<TextBox Height="30"></TextBox>
<Button Style="{StaticResource NewButton}" Height="30"/>
</StackPanel>
</Grid>
</Page>
运行后的效果如下图:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<Style x:Key="BASE">
<Setter Property="Control.Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Offset="0" Color="Green"/>
<GradientStop Offset="0.5" Color="Blue"/>
<GradientStop Offset="1" Color="Red"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
<Style BasedOn="{StaticResource BASE}" TargetType="{x:Type Button}">
<Setter Property="BorderBrush" Value="Red"></Setter>
</Style>
<Style BasedOn="{StaticResource BASE}" TargetType="{x:Type TextBox}">
<Setter Property="BorderBrush" Value="Black"></Setter>
</Style>
<Style BasedOn="{StaticResource {x:Type Button}}" x:Key="NewButton" TargetType="{x:Type Button}">
<Setter Property="Margin" Value="10,0,0,0"></Setter>
</Style>
</Page.Resources>
<Grid>
<StackPanel Width="250" Height="400">
<Button Height="30"></Button>
<TextBox Height="30"></TextBox>
<Button Style="{StaticResource NewButton}" Height="30"/>
</StackPanel>
</Grid>
</Page>