Silverlightbutton图片切换样式实例代码

之前一直做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="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="normalImage">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <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="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="White">
        <local:MyImageButton   Margin="0" ImageHover="Images/全屏鼠标移上.png" ImageNormal="Images/全屏.png" Width="100" Height="100" HorizontalAlignment="Center" VerticalAlignment="Center">           
        </local:MyImageButton>
    </Grid>
</UserControl>

(0)

相关推荐

  • silverlight实现图片局部放大效果的方法

    本文实例讲述了silverlight实现图片局部放大效果的方法.分享给大家供大家参考,具体如下: 很多购物平台中(比如京东购物),浏览产品详情时都有这种效果,前几天看到有朋友问SL能不能实现,当然可以 界面: 1.左侧小图片(用一个矩形Fill一张图片即可) 2.左侧半透明矩形 3.右侧大图片(用一个Canvas设置Clip裁剪可视区域作为蒙板,图片放置在Canvas中即可) 原理: 获取左侧半透明矩形的相对位置,然后动态调整右侧大图的Canvas.Left与Canvas.Top 需要知道以下技

  • c#图片缩放图片剪切功能实现(等比缩放)

    所谓c#图片处理高级应,多数是基于.net framework类库完成 复制代码 代码如下: using system;using system.collections.generic;using system.text;using system.io;using system.drawing;using system.drawing.drawing2d;using system.drawing.imaging; namespace wujian.common{    /// <summary>

  • 深入分析WPF客户端读取高清图片卡以及缩略图的解决方法详解

    在Ftp上传上,有人上传了高清图片,每张图片大约2M.如果使用传统的BitmapImage类,然后绑定 Source 属性的方法,有些电脑在首次会比较卡,一张电脑10秒,4张大约会卡40秒. 所以我先异步的下载图片,得到downloadFileStream对象,然后绑定到BitmapImage类上.例如:System.Windows.Controls.Image photo = new Image{    Width = 100,    Height = 100,    Margin = new

  • C#实现图片放大功能的按照像素放大图像方法

    本文实例讲述了基于Visual C#实现的图片放大功能代码.可以直接放大像素,类似photoshop的图片放大功能,可用于像素的定位及修改,由于使用了指针需要勾选允许不安全代码选项,读者可将其用于自己的项目中! 关于几个参数说明: srcbitmap源图片 multiple图像放大倍数 放大处理后的图片 注意:需要在头部引用:using System.Drawing;using System.Drawing.Imaging; 至于命名空间读者可以自己定义. 主要功能代码如下: using Sys

  • C#图片按比例缩放的实现代码

    复制代码 代码如下: using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imaging; namespace Publics{    public class ImgHelper    {        public static void AdjustPhoto(int toWidth, int toHeight, string filePath, string fromFileName, stri

  • Silverlight将图片转换为byte的实现代码

    最近总是有人问如何通过Silverlight上传图片并保存的后台服务器?众所周知,Silverlight是客户端程序,不能很好与服务器进行"沟通",上传图片的方法呢大致都是通过以下流程: 1.客户端获取图片-->2.转换能够传输的数据-->3.传递数据到服务端-->4.服务端还原数据为图片 第一步和第四步没有问题,问题大多出在第二步和第三步,如何得到客户端和服务端都能识别并且容易传递的数据呢? 如果你使用的客户端和服务端都是.NET方案,这就非常好办,可以通过下面的方

  • WPF实现图片合成或加水印的方法【2种方法】

    本文实例讲述了WPF实现图片合成或加水印的方法.分享给大家供大家参考,具体如下: 最近项目中应用多次应用了图片合成,为了今后方便特此记下. 在WPF下有两种图片合成的方式,一种还是用原来C#提供的GDI+方式,命名空间是System.Drawing 和 System.Drawing.Imaging,另一种是WPF中新添加的API,命名空间是 System.Windows.Media 和 System.Windows.Media.Imaging . 我们来做一个简单的例子,分别用上面的两种方式实现

  • C# 实现的图片盖章功能,支持拖拽、旋转、放缩、保存

    实现图片盖章功能,在图片上点击,增加"图章"小图片,可以拖拽"图章"到任意位置,也可以点击图章右下角园框,令图片跟着鼠标旋转和放缩. 操作方法:1.点击增加"图章"2.选中移动图标3.点中右下角放缩旋转图章. 效果图: 实现代码如下: 1.  窗口Xaml代码 复制代码 代码如下: <Window x:Class="Lenovo.YogaPaster.ImageEditWindow"    xmlns="htt

  • WPF/Silverlight实现图片局部放大的方法分析

    本文实例讲述了WPF/Silverlight实现图片局部放大的方法.分享给大家供大家参考,具体如下: 最近的项目中也要用到一个局部图片放大的功能,前面这篇<silverlight实现图片局部放大效果的方法>虽然已经给出了原理.知识要点.尺寸要点及后端主要代码,但遗憾的是没有给出xaml的代码.这里按照前文中的提示,动手用WPF实践了一下,花了一个小时,终于搞出来了.这篇文章也就算是一个补充吧. 界面如下图所示: 实现的原理和用到的知识点请点击上面的链接,杨大侠已经说的很清楚了.这里主要强调的就

  • Silverlightbutton图片切换样式实例代码

    之前一直做WPF现在开始接触Slilverlight感触很多. 今天做一个Button要求 有两个图片,button默认有一个图片,鼠标over时用另一个图片, 用wpf做的时候写一个template很简单,但silverlight和wpf写起来不一样 记录一下.大概思路是两个image鼠标MouseOver的时候一个Visible一个Collapsed 写的是一个自定义控件,代码和皮肤分离,很简单的一个demo 代码下载:ImageButtonTest.rar 先写一个继承自button的im

  • JavaScript实现简单的图片切换功能(实例代码)

    代码如下所示: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>图片切换</title> <style> *{ margin: 0; padding: 0; } .box{ width: 200px; height: 300px; margin: 50px auto; padding: 20p

  • JavaScript基于SVG的图片切换效果实例代码

    最近太忙了,自动来到rjxy后,不晓得怎么回事,忙的都没时间更博了. 昨天还有个同学跟我说,你好久没更新博客了.. 甚为惭愧~~ 正好12月来了,今天开一篇. 最近上课讲到了 SVG,不晓得同学们理解到没. -_-!!! 图片轮播见的太多,今天就用 SVG 写了一个图片轮播,效果如下. 效果要求 点击控制块,图片切换.切换的时候使用圆形做遮罩,由小到大变化.每次切换的时候,圆的位置随机产生. 主要知识点 1. SVG 的裁切(遮罩),clip-path 的运用. 2. SVG 利用 JS 更改层

  • js鼠标点击图片切换效果实现代码

    本文实例讲述了js鼠标点击图片切换效果实现代码.分享给大家供大家参考.具体如下: 运行效果截图如下: 具体代码如下: html代码: <div id="menuWrapper" class="menuWrapper bg1"> <ul class="menu" id="menu"> <li class="bg1" style="background-position:

  • javascript图片切换综合实例(循环切换、顺序切换)

    本文实例为大家介绍了javascript图片切换的两种方式,循环切换以及顺序切换的实例代码,分享给大家供大家参考,具体内容如下 <html> <head> <meta charset="utf-8"> <style> p{margin:0;} input{border:none;outline: none;margin:0;padding:0;} img{width:300px;height:300px;} #loop{margin-le

  • 微信小程序中的列表切换功能实例代码详解

    感觉这列表切换有点类似于轮播图,而且感觉这代码直接可以拿来用,稍微改一改样式什么的就OK了,列表切换也是用到的地方也很多 wxml中的代码如下: <!-- 标签页面标题 --> <view class="tab"> <view class="tab-item {{tab==0?'active':''}}" bindtap="changeItem" data-item="0">音乐推荐<

  • iOS利用UIScrollView实现图片的缩放实例代码

    本文介绍了iOS利用UIScrollView实现图片的缩放实例代码,分享给大家: 第一步:添加scrollView到控制器中 UIScrollView *scrollView = [[UIScrollView alloc] init]; scrollView.frame = CGRectMake(40, 250, 300, 200); self.scrollView = scrollView; [self.view addSubview:scrollView]; 第二步:添加图片控件到scrol

  • iOS 图片旋转方法实例代码

    通过 CGImage 或 CIImage 旋转特定角度 UIImage可通过CGImage或CIImage初始化,初始化方法分别为init(cgImage: CGImage, scale: CGFloat, orientation: UIImageOrientation)和init(ciImage: CIImage, scale: CGFloat, orientation: UIImageOrientation).通过UIImageOrientation的不同取值,可以使图片旋转90.180.2

  • vue.js 左侧二级菜单显示与隐藏切换的实例代码

    废话不多说了,直接给大家贴代码了, 完整代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>vue点击切换显示隐藏</title> <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script> <style type

  • 简单的jQuery banner图片轮播实例代码

    朋友给我看了一个轮播效果,把js下载一看,丫丫的一个这么简单的效果,这么的就能写个几百行的,严重影响浏览器加载速度嘛,以为人家宽带流量不要钱玩的哦..无奈之下给他写了个简单的,把所有代码放上去都没有超过70行,还有圆角的那个CSS,不是每个浏览器都是圆角的,不过不影响美观,嘻嘻. 先给大家展示下效果图: <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>无标题页</title&

随机推荐