Silverlightbutton圖片切換樣式實(shí)例代碼
之前一直做WPF現(xiàn)在開(kāi)始接觸Slilverlight感觸很多。
今天做一個(gè)Button要求
有兩個(gè)圖片,button默認(rèn)有一個(gè)圖片,鼠標(biāo)over時(shí)用另一個(gè)圖片,
用wpf做的時(shí)候?qū)懸粋€(gè)template很簡(jiǎn)單,但silverlight和wpf寫(xiě)起來(lái)不一樣
記錄一下。大概思路是兩個(gè)image鼠標(biāo)MouseOver的時(shí)候一個(gè)Visible一個(gè)Collapsed
寫(xiě)的是一個(gè)自定義控件,代碼和皮膚分離,很簡(jiǎn)單的一個(gè)demo
代碼下載:ImageButtonTest.rar
先寫(xiě)一個(gè)繼承自button的imagebutton類(lèi)
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));
//鼠標(biāo)移到上面
public ImageSource ImageHover
{
get { return (ImageSource)GetValue(ImageHoverProperty); }
set { SetValue(ImageHoverProperty, value); }
}
//默認(rèn)圖片
public ImageSource ImageNormal
{
get { return (ImageSource)GetValue(ImageNormalProperty); }
set { SetValue(ImageNormalProperty, value); }
}
public MyImageButton()
{
this.DefaultStyleKey = typeof(MyImageButton);
}
}
}
一個(gè)是鼠標(biāo)移到上面的imageSource一個(gè)是默認(rèn)的source
看一下它的樣式 用sotryboard控制
鼠標(biāo)MouseOver的時(shí)候一個(gè)Visible一個(gè)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>
這樣就可以用了
我們?cè)陧?yè)面上調(diào)用一下
<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/全屏鼠標(biāo)移上.png" ImageNormal="Images/全屏.png" Width="100" Height="100" HorizontalAlignment="Center" VerticalAlignment="Center">
</local:MyImageButton>
</Grid>
</UserControl>
相關(guān)文章
.Net與JS時(shí)間日期格式的轉(zhuǎn)換問(wèn)題對(duì)比分析
這篇文章主要介紹了.Net與JS時(shí)間日期格式的轉(zhuǎn)換問(wèn)題,結(jié)合實(shí)例形式對(duì)比分析了JS與.Net針對(duì)時(shí)間日期格式的轉(zhuǎn)換處理相關(guān)技巧,需要的朋友可以參考下2016-08-08Asp.net TextBox的TextChanged事件使用介紹
動(dòng)態(tài)創(chuàng)建的控件是如何加載視圖狀態(tài),還提到ProcessPostData方法的調(diào)用,這里我就用TextBox的TextChanged事件來(lái)說(shuō)說(shuō)視圖數(shù)據(jù)的加載以及事件的觸發(fā)2012-12-12asp.net 存儲(chǔ)過(guò)程調(diào)用
調(diào)用存儲(chǔ)過(guò)程,但無(wú)返回值 調(diào)用存儲(chǔ)過(guò)程,返回普通值 調(diào)用存儲(chǔ)過(guò)程,返回?cái)?shù)據(jù)集的實(shí)現(xiàn)代碼。2009-07-07.NET使用Collections.Pooled提升性能優(yōu)化的方法
這篇文章主要介紹了.NET使用Collections.Pooled性能優(yōu)化的方法,今天要給大家分享類(lèi)庫(kù)Collections.Pooled,它是通過(guò)池化內(nèi)存來(lái)達(dá)到降低內(nèi)存占用和GC的目的,另外也會(huì)帶大家看看源碼,為什么它會(huì)帶來(lái)這些性能提升,一起通過(guò)本文學(xué)習(xí)下吧2022-05-05在?.NET?平臺(tái)使用?ReflectionDynamicObject?優(yōu)化反射調(diào)用的代碼詳解
這篇文章主要介紹了在?.NET?平臺(tái)使用?ReflectionDynamicObject?優(yōu)化反射調(diào)用代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03如何在不同.net版本實(shí)現(xiàn)單點(diǎn)登錄
經(jīng)過(guò)研究,重寫(xiě)實(shí)現(xiàn)了一個(gè)可以在不同.net版本中實(shí)現(xiàn)單點(diǎn)登錄的簡(jiǎn)單方法?,F(xiàn)在和大家分享一下,不足之處還望見(jiàn)諒2013-07-07ASP.NET AJAX 1.0 RC開(kāi)發(fā)10分鐘圖解
12月15日,ASP.NET AJAX 1.0 RC版發(fā)布,我下載安裝試用了一下,沒(méi)有寫(xiě)一行代碼,實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的AJAX應(yīng)用,以下為截圖說(shuō)明。2008-03-03asp.net C#實(shí)現(xiàn)解壓縮文件的方法
這篇文章主要介紹了asp.net C#實(shí)現(xiàn)解壓縮文件的方法,分別講述了三種不同的實(shí)現(xiàn)方法,是非常實(shí)用的技巧,需要的朋友可以參考下2014-11-11asp.net SQL存儲(chǔ)過(guò)程分頁(yè)
上周花一下午時(shí)間寫(xiě)了個(gè)分頁(yè).給大家分享下,如果寫(xiě)得不好請(qǐng)大家指出一起討論哈,小弟第一次寫(xiě)文章哈..謝謝.2009-05-05高效的使用 Response.Redirect解決一些不必要的問(wèn)題
這篇文章主要介紹了如何高效的使用 Response.Redirect解決一些不必要的問(wèn)題,需要的朋友可以參考下2014-05-05