<
ControlTemplate
TargetType
=”Button”
>),ControlTemplate内部由Rectangle和ContentPresenter组成。
<
UserControl.Resources
>
2
<
Style
x:Key
=”ButtonStyle1″
TargetType
=”Button”
>
3
<
Setter
Property
=”Template”
>
4
<
Setter.Value
>
5
<
ControlTemplate
TargetType
=”Button”
>
6
<
Grid
>
7
<
Rectangle
RadiusY
=”20″
RadiusX
=”20″
Stroke
=”White”
StrokeThickness
=”8″
>
8
<
Rectangle.Fill
>
9
<
LinearGradientBrush
EndPoint
=”0.5,1″
StartPoint
=”0.5,0″
>
10
<
GradientStop
Color
=”#FFF0F5FD”
Offset
=”0″
/>
11
<
GradientStop
Color
=”#FF009CFF”
Offset
=”1″
/>
12
</
LinearGradientBrush
>
13
</
Rectangle.Fill
>
14
</
Rectangle
>
15
<
ContentPresenter
HorizontalAlignment
=”
{TemplateBinding HorizontalContentAlignment}
“
VerticalAlignment
=”
{TemplateBinding VerticalContentAlignment}
“
/>
16
</
Grid
>
17
</
ControlTemplate
>
18
</
Setter.Value
>
19
</
Setter
>
20
</
Style
>
21
</
UserControl.Resources
>
代码
<
UserControl.Resources
>
2
<
Style
x:Key
=”ButtonStyle1″
TargetType
=”Button”
>
3
<
Setter
Property
=”Template”
>
4
<
Setter.Value
>
5
<
ControlTemplate
TargetType
=”Button”
>
6
<
Grid
>
7
<
VisualStateManager.VisualStateGroups
>
8
<
VisualStateGroup
x:Name
=”CommonStates”
>
9
<
VisualStateGroup.Transitions
>
10
<
VisualTransition
From
=”Normal”
GeneratedDuration
=”0:0:0.3″
To
=”MouseOver”
/>
11
<
VisualTransition
From
=”MouseOver”
GeneratedDuration
=”0:0:0.3″
To
=”Normal”
/>
12
<
VisualTransition
From
=”Pressed”
GeneratedDuration
=”0:0:0.3″
To
=”MouseOver”
/>
13
</
VisualStateGroup.Transitions
>
14
<
VisualState
x:Name
=”Disabled”
>
15
<
Storyboard
>
16
<
DoubleAnimation
Duration
=”0″
To
=”0.5″
Storyboard.TargetProperty
=”(UIElement.Opacity)”
Storyboard.TargetName
=”rectangle”
d:IsOptimized
=”True”
/>
17
<
ColorAnimation
Duration
=”0″
To
=”#FF7BC4F3″
Storyboard.TargetProperty
=”(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)”
Storyboard.TargetName
=”rectangle”
d:IsOptimized
=”True”
/>
18
</
Storyboard
>
19
</
VisualState
>
20
<
VisualState
x:Name
=”Normal”
/>
21
<
VisualState
x:Name
=”MouseOver”
>
22
<
Storyboard
>
23
<
DoubleAnimation
Duration
=”0″
To
=”1″
Storyboard.TargetProperty
=”(UIElement.Opacity)”
Storyboard.TargetName
=”rectangle”
d:IsOptimized
=”True”
/>
24
<
ColorAnimation
Duration
=”0″
To
=”White”
Storyboard.TargetProperty
=”(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)”
Storyboard.TargetName
=”rectangle”
d:IsOptimized
=”True”
/>
25
<
ColorAnimation
Duration
=”0″
To
=”#FF2377AD”
Storyboard.TargetProperty
=”(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)”
Storyboard.TargetName
=”rectangle”
d:IsOptimized
=”True”
/>
26
</
Storyboard
>
27
</
VisualState
>
28
<
VisualState
x:Name
=”Pressed”
>
29
<
Storyboard
>
30
<
ColorAnimation
Duration
=”0″
To
=”#FF09283B”
Storyboard.TargetProperty
=”(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)”
Storyboard.TargetName
=”rectangle”
d:IsOptimized
=”True”
/>
31
</
Storyboard
>
32
</
VisualState
>
33
</
VisualStateGroup
>
34
</
VisualStateManager.VisualStateGroups
>
35
<
Rectangle
x:Name
=”rectangle”
RadiusY
=”20″
RadiusX
=”20″
Stroke
=”White”
StrokeThickness
=”8″
>
36
<
Rectangle.Fill
>
37
<
LinearGradientBrush
EndPoint
=”0.5,1″
StartPoint
=”0.5,0″
>
38
<
GradientStop
Color
=”#FFF0F5FD”
Offset
=”0″
/>
39
<
GradientStop
Color
=”#FF009CFF”
Offset
=”1″
/>
40
</
LinearGradientBrush
>
41
</
Rectangle.Fill
>
42
</
Rectangle
>
43
<
ContentPresenter
HorizontalAlignment
=”
{TemplateBinding HorizontalContentAlignment}
“
VerticalAlignment
=”
{TemplateBinding VerticalContentAlignment}
“
/>
44
</
Grid
>
45
</
ControlTemplate
>
46
</
Setter.Value
>
47
</
Setter
>
48
</
Style
>
49
</
UserControl.Resources
>
private
void
btDemo_Click(
object
sender, System.Windows.RoutedEventArgs e)
2
{
3
//
TODO: Add event handler implementation here.
4
btDemo.Content
=
“
我是测试按钮
“
;
5
}
转载于:https://blog.51cto.com/kevinfan/301226
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/110785.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...