WPF的样式(Style)继承

参考自 http://www.cnblogs.com/SkyD/archive/2008/08/09/1264294.html ,非常感谢。

用Style的BaseOn属性可以实现WPF样式的显式继承,例如,有一个基样式:
ContractedBlock.gif ExpandedBlockStart.gif 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个样式继承它:
ContractedBlock.gif ExpandedBlockStart.gif 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>

而且还可以继续继承上面定义的样式,例如对Button的样式再次扩展:
< Style BasedOn = " {StaticResource {x:Type Button}} "  x:Key = " NewButton "  TargetType = " {x:Type Button} " >
      
< 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 >
完整的代码如下:
ContractedBlock.gif ExpandedBlockStart.gif 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>
运行后的效果如下图:
InheritWPFStyle.jpg
posted on 2008-08-11 16:30 cutebear 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/bear831204/archive/2008/08/11/1265336.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值