WPF中的Style(风格,样式)

本文介绍了如何在WPF中使用Style来统一设置控件样式,包括如何指定Style作用范围、如何利用Trigger实现交互效果等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                                                             WPF中的Style(风格,样式)
                                                                                                            周银辉

在WPF中我们可以使用Style来设置控件的某些属性值,并使该设置影响到指定范围内的所有该类控件或影响指定的某一控件,比如说我们想将窗口中的所有按钮都保持某一种风格,那么我们可以设置一个Style,而不必分别设置每个按钮的风格。

Style是作为一种资源被保存下来的. 看下面的例子:
None.gif <Window.Resources>   
None.gif    
<Style TargetType="Button">
None.gif      
<Setter Property="Foreground"  Value="Blue"/>
None.gif      
<Setter Property="FontFamily " Value="CourierNew"/>
None.gif    
</Style>      
None.gif 
</Window.Resources>
我们声明了一个Style,它被声明在Window.Resources中说明它的有效范围是当前窗体,TargetType="Button" 指示该Style的作用对象是Button类的实例,也就是说在当前窗体中的所有Button实例都将受到该Style的影响(除非某Button有明确地指明它所使用的是另外的Style)。
<Setter Property="Foreground"  Value="Blue"/> 这里的Setter是一个设置器,用来设置该Style要对TargetType的那些属性或对象进行设置,我们这里设置的是Button的Foreground属性,将其值设置为Blue,同理,我们将Button的FontFamily属性设置为CourierNew

这样一来,在默认情况下,被加载到窗口中的所有Button对象都将受到这个Style的影响,从而文本变成统一的蓝色CourierNew字体。
你可以粘贴以下代码到XamlPad中查看效果:
None.gif<Window 
None.gif    
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
None.gif    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
None.gif    Title
="StyleDemo" Height="417" Width="579"
None.gif    
>
None.gif  
None.gif  
None.gif  
<Window.Resources>    
None.gif    
<Style TargetType="Button">
None.gif      
<Setter Property="Foreground"  Value="Blue"/>
None.gif      
<Setter Property="FontFamily " Value="CourierNew"/>
None.gif    
</Style>       
None.gif  
</Window.Resources>
None.gif  
None.gif  
None.gif    
<Grid ShowGridLines="True">
None.gif      
None.gif      
<Grid.ColumnDefinitions>
None.gif        
<ColumnDefinition  Width="50*"/>
None.gif        
<ColumnDefinition Width="50*" />
None.gif      
</Grid.ColumnDefinitions>
None.gif      
<Grid.RowDefinitions>
None.gif        
<RowDefinition  Height="25*"/>
None.gif        
<RowDefinition  Height="25*"/>
None.gif        
<RowDefinition  Height="25*"/>
None.gif      
</Grid.RowDefinitions>
None.gif
None.gif      
<Button Grid.Column="0" Grid.ColumnSpan="1" Grid.Row="0" Grid.RowSpan="1">button1</Button>
None.gif      
<Button Grid.Column="2" Grid.ColumnSpan="1" Grid.Row="1" Grid.RowSpan="1">button2</Button>
None.gif     
None.gif    
</Grid>
None.gif  
None.gif
</Window>
None.gif


接下来很容易想到的一个问题是,想上述代码的强制窗口的所有按钮都受声明的Style的影响是不是有点强奸民意,如果我只想我定义的Style影响指定的Button对象而不是所有的Button对象应该怎么办呢?
参考以下代码:我们为Style添加一个x:Key="ButtonStyle"

None.gif  <Window.Resources>
None.gif    
None.gif    
<Style TargetType="Button" x:Key="ButtonStyle">
None.gif      
<Setter Property="Foreground"  Value="Blue"/>
None.gif      
<Setter Property="FontFamily " Value="CourierNew"/>
None.gif    
</Style>
None.gif        
None.gif  
</Window.Resources>

然后我们使用Button的Style属性来指定该Button所要使用的Style,而其他没有将我们声明的Style指定为其样式的按钮将不受到该Style的影响。
None.gif<Button>normal button</Button>
None.gif
<Button Style="{StaticResource ButtonStyle}">styled button</Button>
这样就很好的解决了Style强制影响每个Button的问题,你可以粘贴以下代码到XamlPad中查看效果:
None.gif<Window 
None.gif    
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
None.gif    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
None.gif    Title
="StyleDemo" Height="417" Width="579"
None.gif    
>
None.gif  
None.gif  
None.gif  
<Window.Resources>   
None.gif    
<Style TargetType="Button" x:Key="ButtonStyle">
None.gif      
<Setter Property="Foreground"  Value="Blue"/>
None.gif      
<Setter Property="FontFamily " Value="CourierNew"/>
None.gif    
</Style>    
None.gif  
</Window.Resources>
None.gif  
None.gif  
None.gif    
<Grid ShowGridLines="True">
None.gif      
None.gif      
<Grid.ColumnDefinitions>
None.gif        
<ColumnDefinition  Width="50*"/>
None.gif        
<ColumnDefinition Width="50*" />
None.gif      
</Grid.ColumnDefinitions>
None.gif      
<Grid.RowDefinitions>
None.gif        
<RowDefinition  Height="25*"/>
None.gif        
<RowDefinition  Height="25*"/>
None.gif        
<RowDefinition  Height="25*"/>
None.gif      
</Grid.RowDefinitions>
None.gif
None.gif      
<Button Grid.Column="0" Grid.ColumnSpan="1" Grid.Row="0" Grid.RowSpan="1">normal button</Button>
None.gif      
<Button Grid.Column="1" Grid.ColumnSpan="1" Grid.Row="1" Grid.RowSpan="1" Style="{StaticResource ButtonStyle}">styled button1</Button>
None.gif      
<Button Grid.Column="0" Grid.ColumnSpan="1" Grid.Row="2" Grid.RowSpan="1" Style="{StaticResource ButtonStyle}">styled button2</Button>
None.gif    
None.gif    
</Grid>
None.gif  
None.gif
</Window>
None.gif


为了让我们的Style对外界的交互做出外观上的相应,比如当鼠标按下时蓝色的文本变成红色,当鼠标松开时文本又恢复蓝色,我们可以在Style中添加Trigger(触发器),除此之外,与类的继承原理相类似,我们还可以使用BaseOn来使一个Style“继承”另一个Style。
参考以下代码:
None.gif <Window.Resources>
None.gif    
None.gif    
<Style TargetType="Button" x:Key="ButtonStyle">
None.gif      
<Setter Property="Foreground"  Value="Blue"/>
None.gif      
<Setter Property="FontFamily " Value="CourierNew"/>
None.gif    
</Style>
None.gif    
None.gif    
<Style TargetType="Button" x:Key="TriggerButtonStyle" BasedOn="{StaticResource ButtonStyle}">
None.gif      
<Style.Triggers>
None.gif        
<Trigger  Property="IsPressed" Value="True">
None.gif          
<Setter Property="Foreground" Value="Red"/>
None.gif        
</Trigger>
None.gif      
</Style.Triggers>
None.gif    
</Style>
None.gif    
None.gif  
</Window.Resources>
我们所声明的第二个Style,即TriggerButtonStyle,它“继承”于ButtonStyle,那么TriggerButtonStyle将会从ButtonStyle那里得到蓝色CourierNew文本的性质。然后我们使用了Trigger来响应鼠标按下,  <Trigger  Property="IsPressed" Value="True"> 表示当Button的IsPressed属性值变为True的时候,将做如下设置<Setter Property="Foreground" Value="Red"/>,即将Button的Foreground属性设置为Red。这里有一个隐含的意思是:当当Button的IsPressed属性值变为False的时候,Foreground属性将恢复原值。
你可以粘贴以下代码到XamlPad中查看效果:
None.gif<Window 
None.gif    
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
None.gif    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
None.gif    Title
="StyleDemo" Height="417" Width="579"
None.gif    
>
None.gif  
None.gif  
None.gif  
<Window.Resources>
None.gif    
None.gif    
<Style TargetType="Button" x:Key="ButtonStyle">
None.gif      
<Setter Property="Foreground"  Value="Blue"/>
None.gif      
<Setter Property="FontFamily " Value="CourierNew"/>
None.gif    
</Style>
None.gif    
None.gif    
<Style TargetType="Button" x:Key="TriggerButtonStyle" BasedOn="{StaticResource ButtonStyle}">
None.gif      
<Style.Triggers>
None.gif        
<Trigger  Property="IsPressed" Value="True">
None.gif          
<Setter Property="Foreground" Value="Red"/>
None.gif        
</Trigger>
None.gif      
</Style.Triggers>
None.gif    
</Style>
None.gif    
None.gif  
</Window.Resources>
None.gif  
None.gif  
None.gif    
<Grid ShowGridLines="True">
None.gif      
None.gif      
<Grid.ColumnDefinitions>
None.gif        
<ColumnDefinition  Width="50*"/>
None.gif        
<ColumnDefinition Width="50*" />
None.gif      
</Grid.ColumnDefinitions>
None.gif      
<Grid.RowDefinitions>
None.gif        
<RowDefinition  Height="25*"/>
None.gif        
<RowDefinition  Height="25*"/>
None.gif        
<RowDefinition  Height="25*"/>
None.gif      
</Grid.RowDefinitions>
None.gif
None.gif      
<Button Grid.Column="0" Grid.ColumnSpan="1" Grid.Row="0" Grid.RowSpan="1">normal button</Button>
None.gif      
<Button Grid.Column="1" Grid.ColumnSpan="1" Grid.Row="1" Grid.RowSpan="1" Style="{StaticResource ButtonStyle}">styled button</Button>
None.gif      
<Button Grid.Column="0" Grid.ColumnSpan="1" Grid.Row="2" Grid.RowSpan="1" Style="{StaticResource TriggerButtonStyle}">trigger button</Button>
None.gif    
None.gif    
</Grid>
None.gif  
None.gif
</Window>
None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值