Silverlight button 图片切换样式

简介: 之前一直做WPF现在开始接触Slilverlight感触很多。 今天做一个Button要求 有两个图片,button默认有一个图片,鼠标over时用另一个图片, 用wpf做的时候写一个template很简单,但silverlight和wpf写起来不一样 记录一下。

之前一直做WPF现在开始接触Slilverlight感触很多。

今天做一个Button要求

有两个图片,button默认有一个图片,鼠标over时用另一个图片,

用wpf做的时候写一个template很简单,但silverlight和wpf写起来不一样

记录一下。大概思路是两个image鼠标MouseOver的时候一个Visible一个Collapsed

写的是一个自定义控件,代码和皮肤分离,很简单的一个demo

代码下载:ImageButtonTest.rar

先写一个继承自button的imagebutton类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Net;
 5 using System.Windows;
 6 using System.Windows.Controls;
 7 using System.Windows.Documents;
 8 using System.Windows.Input;
 9 using System.Windows.Media;
10 using System.Windows.Media.Animation;
11 using System.Windows.Shapes;
12 
13 namespace ImageButtonTest
14 {
15     /// <summary>
16     /// build by lp
17     /// </summary>
18     public class MyImageButton : Button
19     {
20         
21         public static readonly DependencyProperty ImageNormalProperty =
22             DependencyProperty.Register("ImageNormal",
23                                         typeof(ImageSource),
24                                         typeof(MyImageButton),
25                                         new PropertyMetadata(null));
26 
27         public static readonly DependencyProperty ImageHoverProperty =
28             DependencyProperty.Register("ImageHover",
29                                         typeof(ImageSource),
30                                         typeof(MyImageButton),
31                                         new PropertyMetadata(null));
32         //鼠标移到上面
33         public ImageSource ImageHover
34         {
35             get { return (ImageSource)GetValue(ImageHoverProperty); }
36             set { SetValue(ImageHoverProperty, value); }
37         }
38         //默认图片
39         public ImageSource ImageNormal
40         {
41             get { return (ImageSource)GetValue(ImageNormalProperty); }
42             set { SetValue(ImageNormalProperty, value); }
43         }
44         
45         public MyImageButton()
46         {
47             this.DefaultStyleKey = typeof(MyImageButton);
48         }
49     }
50 }

 

 一个是鼠标移到上面的imageSource一个是默认的source

看一下它的样式 用sotryboard控制

鼠标MouseOver的时候一个Visible一个Collapsed

 1 <ResourceDictionary
 2     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4     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">
 5 
 6 
 7     <Style TargetType="local:MyImageButton">
 8         <Setter Property="Template">
 9             <Setter.Value>
10                 <ControlTemplate TargetType="local:MyImageButton">
11                     <Grid Background="{TemplateBinding Background}">
12                         <VisualStateManager.VisualStateGroups>
13                             <VisualStateGroup x:Name="CommonStates">
14                                 
15                                 <VisualState x:Name="Normal"/>
16                                 <VisualState x:Name="MouseOver">
17                                     <Storyboard>
18                                         <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="mouseOverImage">
19                                             <DiscreteObjectKeyFrame KeyTime="0">
20                                                 <DiscreteObjectKeyFrame.Value>
21                                                     <Visibility>Visible</Visibility>
22                                                 </DiscreteObjectKeyFrame.Value>
23                                             </DiscreteObjectKeyFrame>
24                                         </ObjectAnimationUsingKeyFrames>
25                                         <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="normalImage">
26                                             <DiscreteObjectKeyFrame KeyTime="0">
27                                                 <DiscreteObjectKeyFrame.Value>
28                                                     <Visibility>Collapsed</Visibility>
29                                                 </DiscreteObjectKeyFrame.Value>
30                                             </DiscreteObjectKeyFrame>
31                                         </ObjectAnimationUsingKeyFrames>
32                                     </Storyboard>
33                                 </VisualState>
34                                 <VisualState x:Name="Pressed"/>
35                                 <VisualState x:Name="Disabled"/>
36                             </VisualStateGroup>
37                         </VisualStateManager.VisualStateGroups>
38                         <Image x:Name="normalImage" Source="{TemplateBinding ImageNormal}" Stretch="Fill"/>
39                         <Image x:Name="mouseOverImage" Source="{TemplateBinding ImageHover}" Stretch="Fill" Visibility="Collapsed"/>
40                     </Grid>
41                 </ControlTemplate>
42             </Setter.Value>
43         </Setter>
44     </Style>
45 </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>

 

 

目录
相关文章
|
网络协议
NTP网络协议校时
实际业务需求中由于两台server的时间不一致。导致漏数据。损失巨大,被user批的不行不行的。还好IT有一台 server 专门用来校时。
483 0
|
4月前
|
安全 网络安全 网络架构
升级到 Windows 11 后 Wi-Fi 无法使用?Windows 11 升级更新后 WiFi 无法上网?
升级到 Windows 11 后可能出现 Wi-Fi 问题?本文提供两种修复方法:一是使用 Win 系统修复工具,二是通过驱动人生更新网卡驱动。同时详解排查网络问题的步骤,包括检查硬件、修复驱动、调整防火墙设置等,助你快速恢复网络连接。
971 0
|
12月前
|
开发框架 缓存 .NET
阿里云轻量应用服务器、经济型e、通用算力型u1实例怎么选?区别及选择参考
在阿里云目前的活动中,价格比较优惠的云服务器有轻量应用服务器2核2G3M带宽68元1年,经济型e实例2核2G3M带宽99元1年,通用算力型u1实例2核4G5M带宽199元1年,这几个云服务器是用户关注度最高的。有的新手用户由于是初次使用阿里云服务器,对于轻量应用服务器、经济型e、通用算力型u1实例的相关性能并不是很清楚,本文为大家做个简单的介绍和对比,以供参考。
|
存储 NoSQL 关系型数据库
数据库管理系统
【10月更文挑战第8天】
721 1
|
机器学习/深度学习 自然语言处理 语音技术
迁移学习(Transfer Learning)
迁移学习是一种机器学习技术,通过将一个任务中学到的知识应用于另一个相关任务,有效解决了数据稀缺和计算资源有限的问题。它涉及预训练模型、特征提取、微调、领域适应等多种技术,广泛应用于计算机视觉、自然语言处理等领域,显著提升了模型的泛化能力和新任务的性能。
|
Kubernetes Shell Docker
在K8S中,如果容器没有bash命令,如何进⼊容器排查问题?
在K8S中,如果容器没有bash命令,如何进⼊容器排查问题?
|
人工智能 自然语言处理 搜索推荐
【知识图谱】人工智能之知识图谱的详细介绍
知识图谱(Knowledge Graph)作为一种新型的知识表示和组织方式,正逐渐成为信息领域的研究热点。以下是对知识图谱的详细介绍
1459 1
|
安全 编译器 C++
[Eigen中文文档] 矩阵与向量运算
本文章旨在提供有关如何使用 Eigen 在矩阵、向量和标量之间执行算术操作的概述和一些详细信息。
855 0
|
JSON JavaScript 前端开发
【Python】一文带你了解并使用 Json 模块
python的json模块是python官方提供的一个用于解析和生成JSON数据格式的库。JSON是JavaScript对象表示法(JavaScript Object Notation)的缩写,是一种轻量级的数据交换格式,常被用于Web应用程序中,也被广泛地应用于非Web应用程序中。python的json库可以方便地将python中的数据转换为JSON格式数据,并支持将JSON格式数据转换为python中的数据类型。
1030 0
【Python】一文带你了解并使用 Json 模块
|
物联网
使用MQTT客户端连接阿里云MQTT服务器
物联网全栈教程-从云端到设备(八) 一 这一篇文章零妖老哥将给你展示两个电脑软件的使用方法,将极大地方便你调试与MQTT有关的物联网项目。一个叫MQTT客户端用来模拟设备向云端发送数据和接收云端的数据;另一个叫作MQTT单片机编程小工具,是技小新针对阿里云MQTT服务器连接过程中的痛点,自己编写的一个电脑程序,用来生成连接阿里云MQTT服务器时的账号密码等信息的。
50330 50