`
lovnet
  • 浏览: 6714471 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

wpf style入门

阅读更多

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

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

Style是作为一种资源被保存下来的. 看下面的例子:

<Window.Resources>
<StyleTargetType="Button">
<SetterProperty="Foreground"Value="Blue"/>
<SetterProperty="FontFamily"Value="CourierNew"/>
</Style>
</Window.Resources>

我们声明了一个Style,它被声明在Window.Resources中说明它的有效范围是当前窗体,TargetType="Button" 指示该Style的作用对象是Button类的实例,也就是说在当前窗体中的所有Button实例都将受到该Style的影响(除非某Button有明确地指明它所使用的是另外的Style)。
<SetterProperty="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>
<StyleTargetType="Button">
<SetterProperty="Foreground"Value="Blue"/>
<SetterProperty="FontFamily"Value="CourierNew"/>
</Style>
</Window.Resources>


<GridShowGridLines="True">

<Grid.ColumnDefinitions>
<ColumnDefinitionWidth="50*"/>
<ColumnDefinitionWidth="50*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinitionHeight="25*"/>
<RowDefinitionHeight="25*"/>
<RowDefinitionHeight="25*"/>
</Grid.RowDefinitions>

<ButtonGrid.Column="0"Grid.ColumnSpan="1"Grid.Row="0"Grid.RowSpan="1">button1</Button>
<ButtonGrid.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>

<StyleTargetType="Button"x:Key="ButtonStyle">
<SetterProperty="Foreground"Value="Blue"/>
<SetterProperty="FontFamily"Value="CourierNew"/>
</Style>

</Window.Resources>

然后我们使用Button的Style属性来指定该Button所要使用的Style,而其他没有将我们声明的Style指定为其样式的按钮将不受到该Style的影响。

<Button>normalbutton</Button>
<ButtonStyle="{StaticResourceButtonStyle}">styledbutton</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>
<StyleTargetType="Button"x:Key="ButtonStyle">
<SetterProperty="Foreground"Value="Blue"/>
<SetterProperty="FontFamily"Value="CourierNew"/>
</Style>
</Window.Resources>


<GridShowGridLines="True">

<Grid.ColumnDefinitions>
<ColumnDefinitionWidth="50*"/>
<ColumnDefinitionWidth="50*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinitionHeight="25*"/>
<RowDefinitionHeight="25*"/>
<RowDefinitionHeight="25*"/>
</Grid.RowDefinitions>

<ButtonGrid.Column="0"Grid.ColumnSpan="1"Grid.Row="0"Grid.RowSpan="1">normalbutton</Button>
<ButtonGrid.Column="1"Grid.ColumnSpan="1"Grid.Row="1"Grid.RowSpan="1"Style="{StaticResourceButtonStyle}">styledbutton1</Button>
<ButtonGrid.Column="0"Grid.ColumnSpan="1"Grid.Row="2"Grid.RowSpan="1"Style="{StaticResourceButtonStyle}">styledbutton2</Button>

</Grid>

</Window>



为了让我们的Style对外界的交互做出外观上的相应,比如当鼠标按下时蓝色的文本变成红色,当鼠标松开时文本又恢复蓝色,我们可以在Style中添加Trigger(触发器),除此之外,与类的继承原理相类似,我们还可以使用BaseOn来使一个Style“继承”另一个Style。
参考以下代码:

<Window.Resources>

<StyleTargetType="Button"x:Key="ButtonStyle">
<SetterProperty="Foreground"Value="Blue"/>
<SetterProperty="FontFamily"Value="CourierNew"/>
</Style>

<StyleTargetType="Button"x:Key="TriggerButtonStyle"BasedOn="{StaticResourceButtonStyle}">
<Style.Triggers>
<TriggerProperty="IsPressed"Value="True">
<SetterProperty="Foreground"Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>

</Window.Resources>

我们所声明的第二个Style,即TriggerButtonStyle,它“继承”于ButtonStyle,那么TriggerButtonStyle将会从ButtonStyle那里得到蓝色CourierNew文本的性质。然后我们使用了Trigger来响应鼠标按下,<TriggerProperty="IsPressed"Value="True"> 表示当Button的IsPressed属性值变为True的时候,将做如下设置<SetterProperty="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>

<StyleTargetType="Button"x:Key="ButtonStyle">
<SetterProperty="Foreground"Value="Blue"/>
<SetterProperty="FontFamily"Value="CourierNew"/>
</Style>

<StyleTargetType="Button"x:Key="TriggerButtonStyle"BasedOn="{StaticResourceButtonStyle}">
<Style.Triggers>
<TriggerProperty="IsPressed"Value="True">
<SetterProperty="Foreground"Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>

</Window.Resources>


<GridShowGridLines="True">

<Grid.ColumnDefinitions>
<ColumnDefinitionWidth="50*"/>
<ColumnDefinitionWidth="50*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinitionHeight="25*"/>
<RowDefinitionHeight="25*"/>
<RowDefinitionHeight="25*"/>
</Grid.RowDefinitions>

<ButtonGrid.Column="0"Grid.ColumnSpan="1"Grid.Row="0"Grid.RowSpan="1">normalbutton</Button>
<ButtonGrid.Column="1"Grid.ColumnSpan="1"Grid.Row="1"Grid.RowSpan="1"Style="{StaticResourceButtonStyle}">styledbutton</Button>
<ButtonGrid.Column="0"Grid.ColumnSpan="1"Grid.Row="2"Grid.RowSpan="1"Style="{StaticResourceTriggerButtonStyle}">triggerbutton</Button>

</Grid>

</Window>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics