按钮,最终效果,如下图:

见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 自定义按钮的外观的更多相关文章

  1. WPF自定义控件与样式(2)-自定义按钮FButton

    一.前言.效果图 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 还是先看看效果 ...

  2. Web jquery表格组件 JQGrid 的使用 - 5.Pager翻页、搜索、格式化、自定义按钮

    系列索引 Web jquery表格组件 JQGrid 的使用 - 从入门到精通 开篇及索引 Web jquery表格组件 JQGrid 的使用 - 4.JQGrid参数.ColModel API.事件 ...

  3. django xadmin 插件(3) 列表视图新增自定义按钮

    效果图: 编辑按钮是默认的list_editable属性对应的插件(xadmin.plugins.editable) 放大按钮对应的是自定义插件. 自定义按钮源码: xplugin.py(保证能够直接 ...

  4. jqGrid选中行、格式化、自定义按钮、隐藏

    获取选择一行的id: var id=$('#jqGrid').jqGrid('getGridParam','selrow'); 获取选择多行的id: var ids=$('#jqGrid').jqGr ...

  5. WPF -- 自定义按钮

    本文介绍WPF一种自定义按钮的方法. 实现效果 使用图片做按钮背景: 自定义鼠标进入时效果: 自定义按压效果: 自定义禁用效果 实现效果如下图所示: 实现步骤 创建CustomButton.cs,继承 ...

  6. wordpress优化之结合prism.js为编辑器自定义按钮转化代码

    原文链接 http://ymblog.net/2016/07/24/wordpress-prism/ 继昨天花了一天一夜的时间匆匆写了主题Jiameil3.0之后,心中一直在想着优化加速,体验更好,插 ...

  7. iOS开发——UI进阶篇(十八)核心动画小例子,转盘(裁剪图片、自定义按钮、旋转)图片折叠、音量震动条、倒影、粒子效果

    一.转盘(裁剪图片.自定义按钮.旋转) 1.裁剪图片 将一张大图片裁剪为多张 // CGImageCreateWithImageInRect:用来裁剪图片 // image:需要裁剪的图片 // re ...

  8. 网站上点击自定义按钮发起QQ聊天的解决方案

    一.背景 最近由于开发需要,需要在网站上自定义一个立即交谈的按钮,现将解决方式分享给大家. 二.解决方案 1.首先访问:http://shang.qq.com/widget/consult.php,适 ...

  9. 用仿ActionScript的语法来编写html5——第七篇,自定义按钮

    第七篇,自定义按钮这次弄个简单点的,自定义按钮.其实,有了前面所定义的LSprite,LBitmap等类,定义按钮就很方便了.下面是添加按钮的代码, function gameInit(event){ ...

随机推荐

  1. HDU 4921 Map

    题意: 给n个节点  他们形成了最多10条链  每条最多1000的长度  每一个节点有个val  你能够选择任何位置截断链  断点前的全部节点被你获得  通过题中计算公式得出你的val  问  通过随 ...

  2. oracle数据库、客户端安装以及ps/sql连接和导入表实例

    从下面的网址下载http://www.oracle.com/technetwork/database/enterprise-edition/downloads/112010-win32soft-098 ...

  3. codeforces584B Kolya and Tanya

    题目链接:http://codeforces.com/problemset/problem/584/B 解题思路:当n=1时,_______    _______   ______  三个数每位上可以 ...

  4. 解决ERROR 2002 (HY000): Can&#39;t connect to local MySQL server through socket &#39;/tmp/mysql.sock&#39; (2)

    在Mac和XAMPP环境,假设终端打字mysql,现这样的问题: ERROR 2002 (HY000): Can't connect to local MySQL server through soc ...

  5. .Net程序猿乐Android开发---(4)注册页面布局

    接下来我们介绍的登陆页面布局,在本节中,我们看一下注册页面布局,页面布局大同小异,来一起熟悉下基本控件的使用方法. 效果图: 1.加入注冊页面 右键选中layout目录,加入注冊页面.例如以下图 点击 ...

  6. 杭州电ACM1098——Ignatius&#39;s puzzle

    这个话题.简单的数学. 对于函数,f(x)=5*x^13+13*x^5+k*a*x,输入k,对于休闲x,一个数字的存在a,使f(x)是65可分. 对于休闲x. 因此,当x = 1时间,f(x) = 1 ...

  7. HADOOP2.6

    LINUX下HADOOP2.6.0集群环境的搭建 本文旨在提供最基本的,可以用于在生产环境进行Hadoop.HDFS分布式环境的搭建,对自己是个总结和整理,也能方便新人学习使用. 基础环境 JDK的安 ...

  8. Music Studio项目心得--JNI实现C++调用JAVA

    这个项目是我參加内蒙古挑战杯的比赛项目,因为时间关系,我没时间实现OpenOMR开源项目由JAVA全然向C++的转换,经过我半个多月的尝试,我将OpenOMR中的1/3的代码改写成C++,只是非常快我 ...

  9. JAVA的class打包成dll

    一.将已经编译后的java中Class文件进行打包:打包命令JAR 如:将某目录下的所有class文件夹全部进行打包处理: 使用的命令:jar cvf test.jar -C com/ . //注意这 ...

  10. SWT的CheckBoxTreeView的上级菜单与下级菜单的选中的实现

    是不是很神奇? treeViewer.addCheckStateListener(new ICheckStateListener() { @Override public void checkStat ...