濮阳杆衣贸易有限公司

主頁 > 知識庫 > Silverlightbutton圖片切換樣式實例代碼

Silverlightbutton圖片切換樣式實例代碼

熱門標簽:手機地圖標注如何刪除 外呼系統費用一年 外呼系統代理品牌 怎么給超市做地圖標注入駐店 十堰正規(guī)電銷機器人系統 巫師3為什么地圖標注的財寶沒有 世紀佳緣地圖標注怎么去掉 辦理400電話證件 寧波自動外呼系統代理

之前一直做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>

您可能感興趣的文章:
  • Silverlight實現星星閃爍動畫
  • Silverlight實現跑馬燈動畫

標簽:嘉興 天門 平涼 山西 泰州 通遼 景德鎮(zhèn) 牡丹江

巨人網絡通訊聲明:本文標題《Silverlightbutton圖片切換樣式實例代碼》,本文關鍵詞  Silverlightbutton,圖片,切換,;如發(fā)現本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Silverlightbutton圖片切換樣式實例代碼》相關的同類信息!
  • 本頁收集關于Silverlightbutton圖片切換樣式實例代碼的相關信息資訊供網民參考!
  • 推薦文章
    陆河县| 司法| 洪雅县| 昌黎县| 巴林左旗| 上杭县| 江阴市| 承德市| 广安市| 南靖县| 奉节县| 九台市| 剑川县| 东阿县| 卢龙县| 章丘市| 盐源县| 金山区| 陆丰市| 仁布县| 元阳县| 辛集市| 铁岭市| 安达市| 屏南县| 炉霍县| 龙泉市| 安阳县| 寻乌县| 英德市| 堆龙德庆县| 济源市| 曲沃县| 玉屏| 枣阳市| 繁昌县| 安多县| 龙泉市| 封丘县| 南漳县| 高台县|