C#

WPF中的Style

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

Style是作为一种资源被保存下来的. 看下面的例子: <window.Resources>
<style TargetType=”Button”>
<setter Property=”Foreground” Value=”Blue”/>
<setter Property=”FontFamily ” Value=”CourierNew”/>
</style>
</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中查看效果:<window
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
Title=”StyleDemo” Height=”417″ Width=”579″
>
<window.Resources>
<style TargetType=”Button”>
<setter Property=”Foreground” Value=”Blue”/>
<setter Property=”FontFamily ” Value=”CourierNew”/>
</style>
</window.Resources>
<grid ShowGridLines=”True”>
<grid.ColumnDefinitions>
<columnDefinition Width=”50*”/>
<columnDefinition Width=”50*” />
</grid.ColumnDefinitions>
<grid.RowDefinitions>
<rowDefinition Height=”25*”/>
<rowDefinition Height=”25*”/>
<rowDefinition Height=”25*”/>
</grid.RowDefinitions>
<button Grid.Column=”0″ Grid.ColumnSpan=”1″ Grid.Row=”0″ Grid.RowSpan=”1″>button1</button>
<button Grid.Column=”2″ Grid.ColumnSpan=”1″ Grid.Row=”1″ Grid.RowSpan=”1″>button2</button>
</grid>
</window>接下来很容易想到的一个问题是,想上述代码的强制窗口的所有按钮都受声明的Style的影响是不是有点强奸民意,如果我只想我定义的Style影响指定的Button对象而不是所有的Button对象应该怎么办呢?
参考以下代码:我们为Style添加一个x:Key=”ButtonStyle” <window.Resources>
<style TargetType=”Button” x:Key=”ButtonStyle”>
<setter Property=”Foreground” Value=”Blue”/>
<setter Property=”FontFamily ” Value=”CourierNew”/>
</style>
</window.Resources>
然后我们使用Button的Style属性来指定该Button所要使用的Style,而其他没有将我们声明的Style指定为其样式的按钮将不受到该Style的影响。
<button>normal button</button>
<button Style=”{StaticResource ButtonStyle}”>styled button</button>
这样就很好的解决了Style强制影响每个Button的问题,你可以粘贴以下代码到XamlPad中查看效果:
<window
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
Title=”StyleDemo” Height=”417″ Width=”579″
>
<window.Resources>
<style TargetType=”Button” x:Key=”ButtonStyle”>
<setter Property=”Foreground” Value=”Blue”/>
<setter Property=”FontFamily ” Value=”CourierNew”/>
</style>
</window.Resources>
<grid ShowGridLines=”True”>
<grid.ColumnDefinitions>
<columnDefinition Width=”50*”/>
<columnDefinition Width=”50*” />
</grid.ColumnDefinitions>
<grid.RowDefinitions>
<rowDefinition Height=”25*”/>
<rowDefinition Height=”25*”/>
<rowDefinition Height=”25*”/>
</grid.RowDefinitions>
<button Grid.Column=”0″ Grid.ColumnSpan=”1″ Grid.Row=”0″ Grid.RowSpan=”1″>normal button</button>
<button Grid.Column=”1″ Grid.ColumnSpan=”1″ Grid.Row=”1″ Grid.RowSpan=”1″ Style=”{StaticResource ButtonStyle}”>styled button1</button>
<button Grid.Column=”0″ Grid.ColumnSpan=”1″ Grid.Row=”2″ Grid.RowSpan=”1″ Style=”{StaticResource ButtonStyle}”>styled button2</button>
</grid>
</window>为了让我们的Style对外界的交互做出外观上的相应,比如当鼠标按下时蓝色的文本变成红色,当鼠标松开时文本又恢复蓝色,我们可以在Style中添加Trigger(触发器),除此之外,与类的继承原理相类似,我们还可以使用BaseOn来使一个Style“继承”另一个Style。
参考以下代码: <window.Resources>
<style TargetType=”Button” x:Key=”ButtonStyle”>
<setter Property=”Foreground” Value=”Blue”/>
<setter Property=”FontFamily ” Value=”CourierNew”/>
</style>
<style TargetType=”Button” x:Key=”TriggerButtonStyle” BasedOn=”{StaticResource ButtonStyle}”>
<style.Triggers>
<trigger Property=”IsPressed” Value=”True”>
<setter Property=”Foreground” Value=”Red”/>
</trigger>
</style.Triggers>
</style>
</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中查看效果:<window
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
Title=”StyleDemo” Height=”417″ Width=”579″
>
<window.Resources>
<style TargetType=”Button” x:Key=”ButtonStyle”>
<setter Property=”Foreground” Value=”Blue”/>
<setter Property=”FontFamily ” Value=”CourierNew”/>
</style>
<style TargetType=”Button” x:Key=”TriggerButtonStyle” BasedOn=”{StaticResource ButtonStyle}”>
<style.Triggers>
<trigger Property=”IsPressed” Value=”True”>
<setter Property=”Foreground” Value=”Red”/>
</trigger>
</style.Triggers>
</style>
</window.Resources>
<grid ShowGridLines=”True”>
<grid.ColumnDefinitions>
<columnDefinition Width=”50*”/>
<columnDefinition Width=”50*” />
</grid.ColumnDefinitions>
<grid.RowDefinitions>
<rowDefinition Height=”25*”/>
<rowDefinition Height=”25*”/>
<rowDefinition Height=”25*”/>
</grid.RowDefinitions>
<button Grid.Column=”0″ Grid.ColumnSpan=”1″ Grid.Row=”0″ Grid.RowSpan=”1″>normal button</button>
<button Grid.Column=”1″ Grid.ColumnSpan=”1″ Grid.Row=”1″ Grid.RowSpan=”1″ Style=”{StaticResource ButtonStyle}”>styled button</button>
<button Grid.Column=”0″ Grid.ColumnSpan=”1″ Grid.Row=”2″ Grid.RowSpan=”1″ Style=”{StaticResource TriggerButtonStyle}”>trigger button</button>
</grid>
</window>

分类: C#

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部