Silverlight button 图片切换样式
之前一直做WPF现在开始接触Slilverlight感触很多。
今天做一个Button要求
有两个图片,button默认有一个图片,鼠标over时用另一个图片,
用wpf做的时候写一个template很简单,但silverlight和wpf写起来不一样
记录一下。大概思路是两个image鼠标MouseOver的时候一个Visible一个Collapsed
写的是一个自定义控件,代码和皮肤分离,很简单的一个demo
代码下载:ImageButtonTest.rar
先写一个继承自button的imagebutton类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes; namespace ImageButtonTest
{
/// <summary>
/// build by lp
/// </summary>
public class MyImageButton : Button
{ public static readonly DependencyProperty ImageNormalProperty =
DependencyProperty.Register("ImageNormal",
typeof(ImageSource),
typeof(MyImageButton),
new PropertyMetadata(null)); public static readonly DependencyProperty ImageHoverProperty =
DependencyProperty.Register("ImageHover",
typeof(ImageSource),
typeof(MyImageButton),
new PropertyMetadata(null));
//鼠标移到上面
public ImageSource ImageHover
{
get { return (ImageSource)GetValue(ImageHoverProperty); }
set { SetValue(ImageHoverProperty, value); }
}
//默认图片
public ImageSource ImageNormal
{
get { return (ImageSource)GetValue(ImageNormalProperty); }
set { SetValue(ImageNormalProperty, value); }
} public MyImageButton()
{
this.DefaultStyleKey = typeof(MyImageButton);
}
}
}
一个是鼠标移到上面的imageSource一个是默认的source
看一下它的样式 用sotryboard控制
鼠标MouseOver的时候一个Visible一个Collapsed
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ImageButtonTest" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Style TargetType="local:MyImageButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:MyImageButton">
<Grid Background="{TemplateBinding Background}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="mouseOverImage">
<DiscreteObjectKeyFrame KeyTime="">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="normalImage">
<DiscreteObjectKeyFrame KeyTime="">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed"/>
<VisualState x:Name="Disabled"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Image x:Name="normalImage" Source="{TemplateBinding ImageNormal}" Stretch="Fill"/>
<Image x:Name="mouseOverImage" Source="{TemplateBinding ImageHover}" Stretch="Fill" Visibility="Collapsed"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
这样就可以用了
我们在页面上调用一下
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ImageButtonTest" x:Class="ImageButtonTest.MainPage"
mc:Ignorable="d"
d:DesignHeight="" d:DesignWidth=""> <Grid x:Name="LayoutRoot" Background="White">
<local:MyImageButton Margin="" ImageHover="Images/全屏鼠标移上.png" ImageNormal="Images/全屏.png" Width="" Height="" HorizontalAlignment="Center" VerticalAlignment="Center">
</local:MyImageButton>
</Grid>
</UserControl>
Silverlight button 图片切换样式的更多相关文章
- javascript图片切换
JavaScript 图片滑动切换效果 作者:cloudgamer 时间: 2009-09-25 文档类型:原创 来自:蓝色理想 第 1 页 JavaScript 图片滑动切换效果 [1] 第 2 页 ...
- Javascript:一款简易的图片切换插件
最近迷上javascript,每天不写点什么都不舒服哈~ 尽管自己能力有限,还是尽自己所能写点东西出来. 实现效果: 效果预览:http://codepen.io/anon/pen/BNjxXj 该插 ...
- jQuery plugin : bgStretcher 背景图片切换效果插件
转自:http://blog.dvxj.com/pandola/jQuery_bgStretcher.html bgStretcher 2011 (Background Stretcher)是一个jQ ...
- 原生js实现多组图片切换
这几天一直在练习原生js写效果,需要理清自己的逻辑,做了一个切换多组图片的效果: css样式: * { margin: 0; padding: 0; } body { background: #303 ...
- JQuery图片切换动画效果
由于博主我懒,所以页面画的比较粗糙,但是没关系,因为我主要讲的是如何实现图片动画切换. 思路:想必大家都逛过淘宝或者其他的一些网站,一般都会有图片动画切换的效果,那是怎样实现的呢?博主我呢,技术不是很 ...
- JS图片切换效果
源地址:http://www.codefans.net/jscss/code/4699.shtml <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1 ...
- zoeDylan.ImgChange 图片切换插件
墨芈深夜发布插件——图片切换 附上下载地址:http://pan.baidu.com/s/17kKF3共享天地/[墨芈 插件]zoeDylan Plugins Code JS (function ($ ...
- CSS学习------之简单图片切换
最近一直在重温纯CSS,学习的时候真的才发现,css真的博大精深啊! 所以趁着学习的劲头,谢了个最简单的CSS图片切换! 先整理下思路: 首先我希望图片居中间,两边有个切换按钮,点击按钮的时候,可以实 ...
- HTML中动态图片切换JQuery实现
相信很多同学都注意到了,各大新闻或者娱乐网站都含有动态图片切换,那个漂亮的感觉让刚刚学习html的人,都非常好奇和心动.那下面就让我们看一下到底如何实现动态图片切换呢?看一下百度贴吧的效果图吧~ // ...
随机推荐
- ASCII
0. American Standard Code of Information Interchange 美国信息交换标准码, 即ASCII, 定义表如下: Bin(二进制) Oct(八进制) Dec ...
- 【SQL查询】集合查询之INTERSECT
[SQL查询]集合查询之INTERSECT 1 BLOG文档结构图 2 前言部分 2.1 导读和注意事项 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知识,~ ...
- C#调用C++ DLL 文件
说来惭愧,都注册一年多了,却没有发表过一篇正式的博文,中间很多学习的过程也没有记录下来.如今到了一个新的环境,也有了学习的机会,一定要把每天的收获记录一下. 要做的东西需要引用C++编写的DLL,刚开 ...
- Linux下使用iostat 监视I/O状态
我们可以使用 sar(1), pidstat(1), mpstat(1), vmstat(8) 来监控 一.安装 yum install sysstat 二.参数解释 FILES /proc/stat ...
- 【mysql】使用脚本对mysql状态进行监控
1.mysqladmin 使用mysqladmin extended-status命令可以获得所有MySQL性能指标,即show global status的输出,不过,因为多数这些指标都是累计值,如 ...
- Web开发须知:URL编码与解码
通常如果一样东西需要编码,说明这样东西并不适合传输.原因多种多样,如Size过大,包含隐私数据,对于Url来说,之所以要进行编码,是因为Url中有些字符会引起歧义. 例如,Url参数字符串中使用key ...
- tomcat发布记录
web项目发布详细步骤 服务器 tomcat服务器1.删除webapps文件夹里面的项目war包-->ifm.war(项目war名称)2.把项目的ifm.war放到webapps里面3.删除we ...
- activiti自定义流程之整合(三):整合自定义表单创建模型
本来在创建了表单之后应该是表单列表和预览功能,但是我看了看整合的代码,和之前没有用angularjs的基本没有什么变化,一些极小的变动也只是基于angularjs的语法,因此完全可以参考之前说些的表单 ...
- html5 实现video标签的自定义播放进度条
现在随着html5的渐热,越来越多的web开发者都开始选择使用html5写出一些比较好的web应用. html代码: <!DOCTYPE html> <html lang=" ...
- sublime Text 2 安装Sublime Package Control
Sublime Text 2 安装 Sublime Package Control,通过 Sublime Package Control,安装.升级和卸载 Package. 安装 Package Co ...