Sliverlight实例之 使用 ControlTemplate 自定义按钮的外观
按钮,最终效果,如下图:

见Project21_ButtonSkin
1, 创建Sliverlight项目

说明:
generic.xaml:样式和模板就被定义在这个文件里
MyButton.cs:控件的逻辑代码
2, 将下面两行代码添加到generic.xaml文件中
xmlns:src="clr-namespace:ButtonControlLibrary;assembly=ButtonControlLibrary"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"

3,Copy按钮的默认的ControlTemplate
(1) 在Blend for VS 中打开按钮的默认模板和样式

(2) 将(1)中的按钮“默认模板和样式”xaml代码,Copy到generic.xaml文件中
4,创建初始ControlTemplate
(1) 删除 Setter 元素直到(但不包括)<Setter Property="Template">。
(2) 删除第一个 Grid 元素,但不删除它内部的元素。
(3) 删除所有 Storyboard 元素,包括它们内部的元素。
(4) 删除名为 Background 的 Border 元素,包括它内部的元素。
(5) 删除 ContentPresenter 元素。
(6) 删除名为 DisabledVisualElement 和 FocusVisualElement 的 Rectangle 元素。
Border、ContentPresenter 和 Rectangle 元素组成默认按钮控件的结构。
(7) 在 Style 元素中,将 TargetType 属性更改为 src:MyButton。
(8) 在 ControlTemplate 元素中,将 TargetType 属性更改为 src:MyButton。
5,在generic.xaml中,编辑自己的模板和样式
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:ButtonControlLibrary;assembly=ButtonControlLibrary"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"> <!--
一,创建初始 ControlTemplate
二,创建可视结构 step 1,2,3,4,5,6,7
三,根据状态定义外观 step 8,9,10,11,12
四,指定可视行为 step 13
五,引用样式 step14
-->
<Style x:Key="ButtonStyle1" TargetType="src:MyButton" > <!--step7 设置该按钮的默认属性-->
<Setter Property="Background" Value="Navy"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="40"/>
<Setter Property="Margin" Value="10"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="src:MyButton">
<!-- step1 一个名为 RootElement 的 Border-->
<Border x:Name="RootElement"> <VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<!--step13 使用 VisualTransition 可以指定控件转换为特定状态所耗费的时间。GeneratedDuration 属性指定转换所需的时间 -->
<vsm:VisualStateGroup.Transitions>
<!--指定该按钮应经过百分之一秒才进入按下状态-->
<vsm:VisualTransition To="Pressed" GeneratedDuration="0:0:0.01" />
<!--指定该按钮应经过半秒才进入鼠标悬停状态-->
<vsm:VisualTransition To="MouseOver" GeneratedDuration="0:0:0.5" />
<!-- 指定该按钮应经过百分之一秒才从按下状态进入鼠标悬停状态-->
<vsm:VisualTransition From="Pressed" To="MouseOver" GeneratedDuration="0:0:0.01" /> <!-- 指定当控件从鼠标悬停状态转换为正常状态时某动画产生动作-->
<vsm:VisualTransition From="MouseOver" To="Normal"
GeneratedDuration="0:0:1.5">
<!-- 当用户将鼠标指针从按钮上移开时,按钮的边框在 1.5 秒内先变为蓝色,然后变为黄色,最后变为黑色-->
<Storyboard>
<ColorAnimationUsingKeyFrames
Storyboard.TargetProperty="Color"
Storyboard.TargetName="BorderBrush"
FillBehavior="HoldEnd" >
<ColorAnimationUsingKeyFrames.KeyFrames>
<LinearColorKeyFrame Value="Blue" KeyTime="0:0:0.5" />
<LinearColorKeyFrame Value="Yellow" KeyTime="0:0:1" />
<LinearColorKeyFrame Value="Black" KeyTime="0:0:1.5" />
</ColorAnimationUsingKeyFrames.KeyFrames>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</vsm:VisualTransition> </vsm:VisualStateGroup.Transitions> <VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<!--step8 将鼠标指针移到按钮上方时,按钮边框会设为红色-->
<Storyboard>
<ColorAnimation Storyboard.TargetName="BorderBrush"
Storyboard.TargetProperty="Color" To="Red" />
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<!--step9 当按钮处于按下状态时,按钮边框会设为透明-->
<Storyboard >
<ColorAnimation Storyboard.TargetName="BorderBrush"
Storyboard.TargetProperty="Color" To="Transparent"
/>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled">
<!--step10 这使得当处于禁用状态时,DisabledRect 的 Opacity 会设为 1。这样将会在按钮的 IsEnabled 属性设置为 false 时显示 DisabledRect-->
<Storyboard>
<DoubleAnimation Storyboard.TargetName="DisabledRect"
Storyboard.TargetProperty="Opacity"
To="1" Duration="0" />
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused">
<!--step11 -->
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="FocusVisual"
Storyboard.TargetProperty="Visibility" Duration="0">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard> </VisualState>
<VisualState x:Name="Unfocused">
<!--step12 -->
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="FocusVisual"
Storyboard.TargetProperty="Visibility" Duration="0">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups> <!--step2 按钮的背景-->
<Border.Background>
<SolidColorBrush x:Name="BorderBrush" Color="Black"/>
</Border.Background> <!--step3 作为 RootElement 的子级的 Grid-->
<Grid Background="{TemplateBinding Background}" Margin="4">
<!--step4 指示按钮是否具有焦点的 Rectangle-->
<Rectangle Name="FocusVisual"
Visibility="Collapsed" Margin="2"
Stroke="{TemplateBinding Foreground}" StrokeThickness="1"
StrokeDashArray="1.5 1.5"/>
<!-- step5 一个显示按钮内容的 ContentPresenter-->
<ContentPresenter
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Margin="4,5,4,4" />
<!--step6 在禁用按钮时使按钮变灰的 Rectangle-->
<Rectangle x:Name="DisabledRect"
Fill="#A5FFFFFF"
Opacity="0" IsHitTestVisible="false" />
</Grid> </Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style> <!-- step14 -->
<Style TargetType="src:MyButton" BasedOn="{StaticResource ButtonStyle1}"></Style> </ResourceDictionary>
6,使用自定义控件
在generic.xaml中,引用样式
<!-- step14 -->
<Style TargetType="src:MyButton" BasedOn="{StaticResource ButtonStyle1}"></Style>
在页面中,添加引用
xmlns:blib="clr-namespace:ButtonControlLibrary;assembly=ButtonControlLibrary"
在页面中,创建两个Button
<StackPanel>
<blib:MyButton Content="Button1" />
<blib:MyButton Content="Button2" Background="Purple" />
</StackPanel>
Sliverlight实例之 使用 ControlTemplate 自定义按钮的外观的更多相关文章
- WPF自定义控件与样式(2)-自定义按钮FButton
一.前言.效果图 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 还是先看看效果 ...
- Web jquery表格组件 JQGrid 的使用 - 5.Pager翻页、搜索、格式化、自定义按钮
系列索引 Web jquery表格组件 JQGrid 的使用 - 从入门到精通 开篇及索引 Web jquery表格组件 JQGrid 的使用 - 4.JQGrid参数.ColModel API.事件 ...
- django xadmin 插件(3) 列表视图新增自定义按钮
效果图: 编辑按钮是默认的list_editable属性对应的插件(xadmin.plugins.editable) 放大按钮对应的是自定义插件. 自定义按钮源码: xplugin.py(保证能够直接 ...
- jqGrid选中行、格式化、自定义按钮、隐藏
获取选择一行的id: var id=$('#jqGrid').jqGrid('getGridParam','selrow'); 获取选择多行的id: var ids=$('#jqGrid').jqGr ...
- WPF -- 自定义按钮
本文介绍WPF一种自定义按钮的方法. 实现效果 使用图片做按钮背景: 自定义鼠标进入时效果: 自定义按压效果: 自定义禁用效果 实现效果如下图所示: 实现步骤 创建CustomButton.cs,继承 ...
- wordpress优化之结合prism.js为编辑器自定义按钮转化代码
原文链接 http://ymblog.net/2016/07/24/wordpress-prism/ 继昨天花了一天一夜的时间匆匆写了主题Jiameil3.0之后,心中一直在想着优化加速,体验更好,插 ...
- iOS开发——UI进阶篇(十八)核心动画小例子,转盘(裁剪图片、自定义按钮、旋转)图片折叠、音量震动条、倒影、粒子效果
一.转盘(裁剪图片.自定义按钮.旋转) 1.裁剪图片 将一张大图片裁剪为多张 // CGImageCreateWithImageInRect:用来裁剪图片 // image:需要裁剪的图片 // re ...
- 网站上点击自定义按钮发起QQ聊天的解决方案
一.背景 最近由于开发需要,需要在网站上自定义一个立即交谈的按钮,现将解决方式分享给大家. 二.解决方案 1.首先访问:http://shang.qq.com/widget/consult.php,适 ...
- 用仿ActionScript的语法来编写html5——第七篇,自定义按钮
第七篇,自定义按钮这次弄个简单点的,自定义按钮.其实,有了前面所定义的LSprite,LBitmap等类,定义按钮就很方便了.下面是添加按钮的代码, function gameInit(event){ ...
随机推荐
- DelphiXE7中创建WebService(服务端+客户端) good
相关资料:http://www.2ccc.com/news/Html/?1507.html DelphiXE7新建WebService具体操作:1.打开“DelphiXE7”->“File”-& ...
- Delphi Windows API判断文件共享锁定状态(使用OpenFile来判断)
一.概述 锁是操作系统为实现数据共享而提供的一种安全机制,它使得不同的应用程序,不同的计算机之间可以安全有效地共享和交换数据.要保证安全有效地操作共享数据,必须在相应的操作前判断锁的类型,然后才能确定 ...
- thinkPHP 输出及其模板调用(三)
原文:thinkPHP 输出及其模板调用(三) ThinkPHP 的输出(重点) a.通过 echo 等PHP原生的输出方式在页面中输出 b.通过display方法输出(thinkphp\Home\L ...
- hdu1251(Trie树)
传送门:统计难题 分析:Trie树入门题,随便写写练下手感,统计每个节点被多少单词经过就可以了. #include <iostream> #include <cstdio> # ...
- 在Laravel中一步一步创建Packages
首先要看一下Laravel官方文档,这是最新4.2的文档,假设想看中文的话点击此处,基本一样.这个github上的库setup-laravel4-package,也是一步一步介绍怎样创建一个包.并关联 ...
- Java的图片处理工具类
import Java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...
- VIM命令集
Command Action Notes vim file +54 open file and go to line 54 any : command can be run using + on co ...
- Java文件压缩分割(待)
http://blog.csdn.net/ycg01/article/details/1366648
- SWT入门-常用组件的使用(转)
转自:http://www.cnblogs.com/kentyshang/archive/2007/08/16/858367.html swt的常用组件button ,text ,combo,list ...
- [ACM] POJ 3259 Wormholes (bellman-ford最短路径,推断是否存在负权回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29971 Accepted: 10844 Descr ...