在
上一篇,介绍了Visual State Manager视觉状态管理器,其中涉及到控件的样式(Style)和模板(Template),本篇将详细介绍样式(Style)和模板(Template)在Silverlight项目中的应用,并介绍如何使用Blend设计样式(Style)和模板(Template)。
在LOB(Line-of-Busi
ness)企业级应用项目开发中,为了使项目的视觉
效果多样化,不仅仅使用动画
效果,而且经常还需要
修改部分控件的样式(Style)和模板(Template)。 在Silverlight的控件包中,微软已经定义了
默认控件样式和模板,开发人员可以
在这些
默认的
代码上进行
修改和调整,以达到项目需求。但是由于
默认的控件样式和模板
代码过于冗长,手工
修改起来相对复杂和繁琐,对此,微软封装了一些新的
功能在Blend中,方便开发人员和设计人员对模板和样式进行控制。 在学习Blend控制样式(Style)和模板(Template)前,还是先
快速了解一下样式(Style)和模板(Template)。
样式(Style),有HTML开发基础的朋友对传统的CSS样式表并不陌生,HTML将所有公用的
属性代码汇集到CSS
文件中,使用CSS控制
页面的背景,控制表格的宽度,控制控件距离等。Silverlight中的样式(Style)和CSS相同,允许设计人员和开发人员将控件的公用
属性定义到资源
文件中,允许相同
属性控件自由
调用,这样不仅提高了
代码的
复用性,而且保证了控件外观在项目中的
一致性。
模板(Template),模板的概念在传统的Web页面开发中已经被引入,早期Dreamweaver因为支持创建Web模板设计,吸引了不少Web开发者。Silverlight中同样引进了模板的概念。在Silverlight中,凡是继承自System.Windows.Controls命名控件的控件都有一套默认的模板,设计人员和开发人员可以基于默认模板的基础上进行修改,创建自定义模板。
样式(Style)和模板(Template)的定义
在Silverlight中,样式和模板定义是很简单的,因为很多属性已经被封装好,不需要自行创建,只需要调用就可以了。简单的演示代码:
Style:
1
<
UserControl.Resources
>
@H_502_58@
2
<
Style
x:Key
="TextBoxStyle"
targettype
="TextBox"
>
@H_502_58@
3
这里定义具体样式属性@H_502_58@
4
</
Style
>
@H_502_58@
5
</
UserControl.Resources
>
Template:
1 <UserControl.Resources>
@H_502_58@2 <ControlTemplate x:Key="TextBoxTemplate" targettype="TextBox" >
@H_502_58@3 <Border BorderBrush="Orange" BorderThickness="3" CornerRadius="10"
@H_502_58@4 Background="Red">
@H_502_58@5
这里定义具体模板 @H_502_58@
6 </Border>
@H_502_58@7 </ControlTemplate>
@H_502_58@8 </UserControl.Resources>
样式(Style)和模板(Template)的使用
在Silverlight中样式(Style)和模板(Template)都属于控件资源,也就是说,两者都可以被定义在资源文件中,而在项目页面中,仅需使用Style和Template属性调用就可以了。
Style:
1
<
TextBox
Style
={"StaticResource
TextBoxStyle"} Text
="样式测试"
/>
Template:这个
方法,也是Blend所
支持的
方法,下面我们看看实例,进一步理解。
1
<
TextBox
Template
={"StaticResource
TextBoxTemplate"} Text
="模板测试"
/>
上面介绍了样式和模板的最基本的
用法。
在实际项目中,我们经常把模板(Template)定义在样式(Style)中,也就是将Template作为一个属性被赋值在Style中,这样当样式(Style)被应用在控件中时,新的模板也会同时加载。
例如下面Button样式代码,<Setter Property="Template"> Template是作为Style的属性被设置的:


代码
1
<
ResourceDictionary
>
@H_502_58@
2
<
Style
x:Key
="ButtonStyle1"
targettype
="Button"
>
@H_502_58@
3
<
Setter
Property
="Background"
Value
="#FF1F3B53"
/>
@H_502_58@
4
<
Setter
Property
="Foreground"
Value
="#FF000000"
/>
@H_502_58@
5
<
Setter
Property
="Padding"
Value
="3"
/>
@H_502_58@
6
<
Setter
Property
="BorderThickness"
Value
="1"
/>
@H_502_58@
7
<
Setter
Property
="BorderBrush"
>
@H_502_58@
8
<
Setter.Value
>
@H_502_58@
9
<
LinearGradientBrush
EndPoint
="0.5,1"
StartPoint
="0.5,0"
>
@H_502_58@
10
<
GradientStop
Color
="#FFA3AEB9"
Offset
="0"
/>
@H_502_58@
11
<
GradientStop
Color
="#FF8399A9"
Offset
="0.375"
/>
@H_502_58@
12
<
GradientStop
Color
="#FF718597"
Offset
="0.375"
/>
@H_502_58@
13
<
GradientStop
Color
="#FF617584"
Offset
="1"
/>
@H_502_58@
14
</
LinearGradientBrush
>
@H_502_58@
15
</
Setter.Value
>
@H_502_58@
16
</
Setter
>
@H_502_58@
17
<
Setter
Property
="Template"
>
@H_502_58@
18
<
Setter.Value
>
@H_502_58@
19
<
ControlTemplate
targettype
="Button"
>
@H_502_58@
20
<
Grid
>
@H_502_58@
21
<
visualstatemanager.VisualStateGroups
>
@H_502_58@
22
<
VisualStateGroup
x:Name
="CommonStates"
>
@H_502_58@
23
<
VisualState
x:Name
="normal"
/>
@H_502_58@
24
<
VisualState
x:Name
="MouSEOver"
>
@H_502_58@
25
<
Storyboard
>
@H_502_58@
26
<
DoubleAnimationUsingKeyFrames
Storyboard.TargetName
="BackgroundAnimation"
Storyboard.TargetProperty
="Opacity"
>
@H_502_58@
27
<
SplineDoubleKeyFrame
KeyTime
="0"
Value
="1"
/>
@H_502_58@
28
</
DoubleAnimationUsingKeyFrames
>
@H_502_58@
29
<
ColorAnimationUsingKeyFrames
Storyboard.TargetName
="BackgroundGradient"
Storyboard.TargetProperty
="(Rectangle.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)"
>
@H_502_58@
30
<
SplineColorKeyFrame
KeyTime
="0"
Value
="#F2FFFFFF"
/>
@H_502_58@
31
</
ColorAnimationUsingKeyFrames
>
@H_502_58@
32
<
ColorAnimationUsingKeyFrames
Storyboard.TargetName
="BackgroundGradient"
Storyboard.TargetProperty
="(Rectangle.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)"
>
@H_502_58@
33
<
SplineColorKeyFrame
KeyTime
="0"
Value
="#CCFFFFFF"
/>
@H_502_58@
34
</
ColorAnimationUsingKeyFrames
>
@H_502_58@
35
<
ColorAnimationUsingKeyFrames
Storyboard.TargetName
="BackgroundGradient"
Storyboard.TargetProperty
="(Rectangle.Fill).(GradientBrush.GradientStops)[3].(GradientStop.Color)"
>
@H_502_58@
36
<
SplineColorKeyFrame
KeyTime
="0"
Value
="#7FFFFFFF"
/>
@H_502_58@
37
</
ColorAnimationUsingKeyFrames
>
@H_502_58@
38
</
Storyboard
>
@H_502_58@
39
</
VisualState
>
@H_502_58@
40
<
VisualState
x:Name
="pressed"
>
@H_502_58@
41
<
Storyboard
>
@H_502_58@
42
<
ColorAnimationUsingKeyFrames
Storyboard.TargetName
="Background"
Storyboard.TargetProperty
="(Border.Background).(SolidColorBrush.Color)"
>
@H_502_58@
43
<
SplineColorKeyFrame
KeyTime
="0"
Value
="#FF6DBDD1"
/>
@H_502_58@
44
</
ColorAnimationUsingKeyFrames
>
@H_502_58@
45
<
DoubleAnimationUsingKeyFrames
Storyboard.TargetName
="BackgroundAnimation"
Storyboard.TargetProperty
="Opacity"
>
@H_502_58@
46
<
SplineDoubleKeyFrame
KeyTime
="0"
Value
="1"
/>
@H_502_58@
47
</
DoubleAnimationUsingKeyFrames
>
@H_502_58@
48
<
ColorAnimationUsingKeyFrames
Storyboard.TargetName
="BackgroundGradient"
Storyboard.TargetProperty
="(Rectangle.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)"
>
@H_502_58@
49
<
SplineColorKeyFrame
KeyTime
="0"
Value
="#D8FFFFFF"
/>
@H_502_58@
50
</
ColorAnimationUsingKeyFrames
>
@H_502_58@
51
<
ColorAnimationUsingKeyFrames
Storyboard.TargetName
="BackgroundGradient"
Storyboard.TargetProperty
="(Rectangle.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)"
>
@H_502_58@
52
<
SplineColorKeyFrame
KeyTime
="0"
Value
="#C6FFFFFF"
/>
@H_502_58@
53
</
ColorAnimationUsingKeyFrames
>
@H_502_58@
54
<
ColorAnimationUsingKeyFrames
Storyboard.TargetName
="BackgroundGradient"
Storyboard.TargetProperty
="(Rectangle.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)"
>
@H_502_58@
55
<
SplineColorKeyFrame
KeyTime
="0"
Value
="#8CFFFFFF"
/>
@H_502_58@
56
</
ColorAnimationUsingKeyFrames
>
@H_502_58@
57
<
ColorAnimationUsingKeyFrames
Storyboard.TargetName
="BackgroundGradient"
Storyboard.TargetProperty
="(Rectangle.Fill).(GradientBrush.GradientStops)[3].(GradientStop.Color)"
>
@H_502_58@
58
<
SplineColorKeyFrame
KeyTime
="0"
Value
="#3FFFFFFF"
/>
@H_502_58@
59
</
ColorAnimationUsingKeyFrames
>
@H_502_58@
60
</
Storyboard
>
@H_502_58@
61
</
VisualState
>
@H_502_58@
62
<
VisualState
x:Name
="disabled"
>
@H_502_58@
63
<
Storyboard
>
@H_502_58@
64
<
DoubleAnimationUsingKeyFrames
Storyboard.TargetName
="disabledVisualElement"
Storyboard.TargetProperty
="Opacity"
>
@H_502_58@
65
<
SplineDoubleKeyFrame
KeyTime
="0"
Value
=".55"
/>
@H_502_58@
66
</
DoubleAnimationUsingKeyFrames
>
@H_502_58@
67
</
Storyboard
>
@H_502_58@
68
</
VisualState
>
@H_502_58@
69
</
VisualStateGroup
>
@H_502_58@
70
<
VisualStateGroup
x:Name
="Focusstates"
>
@H_502_58@
71
<
VisualState
x:Name
="Focused"
>
@H_502_58@
72
<
Storyboard
>
@H_502_58@
73
<
DoubleAnimationUsingKeyFrames
Storyboard.TargetName
="FocusVisualElement"
Storyboard.TargetProperty
="Opacity"
>
@H_502_58@
74
<
SplineDoubleKeyFrame
KeyTime
="0"
Value
="1"
/>
@H_502_58@
75
</
DoubleAnimationUsingKeyFrames
>
@H_502_58@
76
</
Storyboard
>
@H_502_58@
77
</
VisualState
>
@H_502_58@
78
<
VisualState
x:Name
="Unfocused"
/>
@H_502_58@
79
</
VisualStateGroup
>
@H_502_58@
80
</
visualstatemanager.VisualStateGroups
>
@H_502_58@
81
<
Border
x:Name
="Background"
Background
="White"
BorderBrush
="
{TemplateBinding BorderBrush}
"
BorderThickness
="
{TemplateBinding BorderThickness}
"
CornerRadius
="3"
>
@H_502_58@
82
<
Grid
Margin
="1"
Background
="
{TemplateBinding Background}
"
>
@H_502_58@
83
<
Border
x:Name
="BackgroundAnimation"
Opacity
="0"
Background
="#FF448DCA"
/>
@H_502_58@
84
<
Rectangle
x:Name
="BackgroundGradient"
>
@H_502_58@
85
<
Rectangle.Fill
>
@H_502_58@
86
<
LinearGradientBrush
EndPoint
=".7,1"
StartPoint
=".7,0"
>
@H_502_58@
87
<
GradientStop
Color
="#FFFFFFFF"
Offset
="0"
/>
@H_502_58@
88
<
GradientStop
Color
="#F9FFFFFF"
Offset
="0.375"
/>
@H_502_58@
89
<
GradientStop
Color
="#E5FFFFFF"
Offset
="0.625"
/>
@H_502_58@
90
<
GradientStop
Color
="#C6FFFFFF"
Offset
="1"
/>
@H_502_58@
91
</
LinearGradientBrush
>
@H_502_58@
92
</
Rectangle.Fill
>
@H_502_58@
93
</
Rectangle
>
@H_502_58@
94
</
Grid
>
@H_502_58@
95
</
Border
>
@H_502_58@
96
<
ContentPresenter
x:Name
="contentPresenter"
HorizontalAlignment
="
{TemplateBinding HorizontalContentAlignment}
"
Margin
="
{TemplateBinding Padding}
"
VerticalAlignment
="
{TemplateBinding VerticalContentAlignment}
"
Content
="
{TemplateBinding Content}
"
ContentTemplate
="
{TemplateBinding ContentTemplate}
"
/>
@H_502_58@
97
<
Rectangle
x:Name
="disabledVisualElement"
Fill
="#FFFFFFFF"
RadiusX
="3"
RadiusY
="3"
IsHitTestVisible
="false"
Opacity
="0"
/>
@H_502_58@
98
<
Rectangle
x:Name
="FocusVisualElement"
stroke
="#FF6DBDD1"
strokeThickness
="1"
RadiusX
="2"
RadiusY
="2"
Margin
="1"
IsHitTestVisible
="false"
Opacity
="0"
/>
@H_502_58@
99
</
Grid
>
@H_502_58@
100
</
ControlTemplate
>
@H_502_58@
101
</
Setter.Value
>
@H_502_58@
102
</
Setter
>
@H_502_58@
103
</
Style
>
@H_502_58@
104
</
ResourceDictionary
>
下面我们用一个简单的实例来理解Style(样式)和Template(模板)。
首先,我们创建
一个新的项目StyleTemplateDemo作为演示。
打开MainPage,在主设计窗口中,
添加三个按钮控件到该
页面,
对于Button控件的
样式属性控制,最简单的
方法就是从右边Properties
属性栏,
点击“[Button]”下拉
菜单,选择“Edit Template”,然后选择“Edit a
copy”,
或者,可以在主设计窗口,使用鼠标右键选中控件,选择“Edit Template”,然后选择“Edit a
copy”,
选中“Edit a
copy”,系统会弹出
提示窗口,询问“Create Style Resource”是否创建样式资源,
其中Name(Key)是样式名,以后
调用该样式将使用这个Name;
Define in 有三个选择:
1. Application(应用),该选项是将该样式
代码添加到App.Xaml
文件中;
2. This document(当前文档),该选项是将该样式
代码添加到当前编辑文档中,当前我们编辑MainPage.Xaml,如果选中此项,样式
代码将被
输出到该
文件中;
在This document(当前文档)中,该选项有两个选择,
“UserControl:<no name>”,选中该选项样式
代码将在当前文档创建<UserControl.Resources>,
1
<
Button
Margin
="114,197,228"
Width
="139"
Content
="Button"
HorizontalAlignment
="Left"
d:LayoutOverrides
="Width"
Style
="
{StaticResource ButtonStyle1}
"
/>
“Button:<no name>”,选中该选项样式
代码将创建在该控件资源下,
这里我们Define in选择Application,定义样式
代码到App.xaml中,点击确定后,系统会打开App.xaml
文件,进入控件样式编辑状态,这时就可以对控件样式进行编辑。
当前主设计窗口打开
文件为"App.xaml",因为该
文件包含了要编辑的样式和模板
代码。
在控件处于样式和模板编辑状态下,从Objects and Timeline窗口可以查看该控件模板的子部件,通过
修改包含的子部件样式,来改变原按钮控件样式。
例如我们想改变Button的背景色,可以
修改BackgroundGradient
属性,从上图的Objects and Timeline中选中BackgroundGradient,
在右边Properties
属性栏,
修改颜色,即可看到在主设计窗口按钮的背景色在改变。
保存以上
修改后,回到主设计窗口,能看到两个Button已经被应用了新的Button样式,
1
<
Grid
x:Name
="LayoutRoot"
Background
="White"
>
@H_502_58@
2
<
Button
x:Name
="bt1"
Margin
="254,151,247,0"
Width
="139"
Content
="按钮样式"
Style
="
{StaticResource ButtonStyle1}
"
Height
="55"
VerticalAlignment
="Top"
/>
@H_502_58@
3
<
Button
Margin
="0,92,0"
Width
="139"
Content
="Button"
HorizontalAlignment
="Right"
d:LayoutOverrides
="Width"
Height
="55"
VerticalAlignment
="Top"
/>
@H_502_58@
4
<
Button
Margin
="94,0"
Width
="139"
Content
="Button"
HorizontalAlignment
="Left"
d:LayoutOverrides
="Width"
Style
="
{StaticResource ButtonStyle1}
"
Height
="55"
VerticalAlignment
="Top"
/>
@H_502_58@
5
</
Grid
>
从
代码中可以看出,使用
Style
="
{StaticResource ButtonStyle1}
" 的按钮,已经将
默认Button样式替换为新的Button样式。如果想
修改最后
一个Button的样式,同样,把
Style
="
{StaticResource ButtonStyle1}
" 属性添加到Button中即可。 在Blend中可以非常简单的应用新的样式到控件。
在需要被应用样式的Button控件上点击鼠标右键,然后选择“Edit Template”->“Apply Resource” ->选中当前需要被应用的样式,在主设计窗口即可看到控件样式的变化。
现在,我们切换左边的Objects and Timeline 到 States,当前选中的States是CommonStates组中的
normal, 阅读过
上一篇VSM教程的,相信对这里并不陌生,
normal表示Button控件的正常视图状态。

这里我们可以看到,在主设计窗口,Button样式仍旧是紫色
当我们选中Mou
SEOver视图状态时,主设计窗口Button样式,变成
默认的样式。这说明,每个Visual State视觉状态都有自己独立样式,和
上一篇讲述的控件每个视图状态都有独立性是相符的。
所以,要
修改完整的一套控件样式,需要以控件的视图状态为基础,
修改每个会引起控件视图产生变化的状态。这里,我们选中Mou
SEOver后,切换Tab到 Objects and Timeline,
选中“BackgroundGradient”,
修改Button控件在鼠标覆盖后的背景,
保存后,F5运行该例程,当鼠标放在Button上,
自动会切换Button样式。
对于控件样式的的控制,基本已经讲完。在
下一篇将介绍Template模板的应用实例。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。