欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Silverlightbutton圖片切換樣式實例代碼

 更新時間:2013年11月01日 15:27:22   作者:  
這篇文章介紹了Silverlightbutton圖片切換樣式實例代碼,有需要的朋友可以參考一下

之前一直做WPF現(xiàn)在開始接觸Slilverlight感觸很多。

今天做一個Button要求

有兩個圖片,button默認有一個圖片,鼠標(biāo)over時用另一個圖片,

用wpf做的時候?qū)懸粋€template很簡單,但silverlight和wpf寫起來不一樣

記錄一下。大概思路是兩個image鼠標(biāo)MouseOver的時候一個Visible一個Collapsed

寫的是一個自定義控件,代碼和皮膚分離,很簡單的一個demo

代碼下載:ImageButtonTest.rar

先寫一個繼承自button的imagebutton類

復(fù)制代碼 代碼如下:

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); }
        }
        //默認圖片
        public ImageSource ImageNormal
        {
            get { return (ImageSource)GetValue(ImageNormalProperty); }
            set { SetValue(ImageNormalProperty, value); }
        }

        public MyImageButton()
        {
            this.DefaultStyleKey = typeof(MyImageButton);
        }
    }
}

一個是鼠標(biāo)移到上面的imageSource一個是默認的source

看一下它的樣式 用sotryboard控制

鼠標(biāo)MouseOver的時候一個Visible一個Collapsed

復(fù)制代碼 代碼如下:

<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>

這樣就可以用了

我們在頁面上調(diào)用一下

復(fù)制代碼 代碼如下:

<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時間日期格式的轉(zhuǎn)換問題對比分析

    .Net與JS時間日期格式的轉(zhuǎn)換問題對比分析

    這篇文章主要介紹了.Net與JS時間日期格式的轉(zhuǎn)換問題,結(jié)合實例形式對比分析了JS與.Net針對時間日期格式的轉(zhuǎn)換處理相關(guān)技巧,需要的朋友可以參考下
    2016-08-08
  • Asp.net TextBox的TextChanged事件使用介紹

    Asp.net TextBox的TextChanged事件使用介紹

    動態(tài)創(chuàng)建的控件是如何加載視圖狀態(tài),還提到ProcessPostData方法的調(diào)用,這里我就用TextBox的TextChanged事件來說說視圖數(shù)據(jù)的加載以及事件的觸發(fā)
    2012-12-12
  • asp.net 存儲過程調(diào)用

    asp.net 存儲過程調(diào)用

    調(diào)用存儲過程,但無返回值 調(diào)用存儲過程,返回普通值 調(diào)用存儲過程,返回數(shù)據(jù)集的實現(xiàn)代碼。
    2009-07-07
  • .NET使用Collections.Pooled提升性能優(yōu)化的方法

    .NET使用Collections.Pooled提升性能優(yōu)化的方法

    這篇文章主要介紹了.NET使用Collections.Pooled性能優(yōu)化的方法,今天要給大家分享類庫Collections.Pooled,它是通過池化內(nèi)存來達到降低內(nèi)存占用和GC的目的,另外也會帶大家看看源碼,為什么它會帶來這些性能提升,一起通過本文學(xué)習(xí)下吧
    2022-05-05
  • 在?.NET?平臺使用?ReflectionDynamicObject?優(yōu)化反射調(diào)用的代碼詳解

    在?.NET?平臺使用?ReflectionDynamicObject?優(yōu)化反射調(diào)用的代碼詳解

    這篇文章主要介紹了在?.NET?平臺使用?ReflectionDynamicObject?優(yōu)化反射調(diào)用代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • 如何在不同.net版本實現(xiàn)單點登錄

    如何在不同.net版本實現(xiàn)單點登錄

    經(jīng)過研究,重寫實現(xiàn)了一個可以在不同.net版本中實現(xiàn)單點登錄的簡單方法?,F(xiàn)在和大家分享一下,不足之處還望見諒
    2013-07-07
  • ASP.NET AJAX 1.0 RC開發(fā)10分鐘圖解

    ASP.NET AJAX 1.0 RC開發(fā)10分鐘圖解

    12月15日,ASP.NET AJAX 1.0 RC版發(fā)布,我下載安裝試用了一下,沒有寫一行代碼,實現(xiàn)了一個簡單的AJAX應(yīng)用,以下為截圖說明。
    2008-03-03
  • asp.net C#實現(xiàn)解壓縮文件的方法

    asp.net C#實現(xiàn)解壓縮文件的方法

    這篇文章主要介紹了asp.net C#實現(xiàn)解壓縮文件的方法,分別講述了三種不同的實現(xiàn)方法,是非常實用的技巧,需要的朋友可以參考下
    2014-11-11
  • asp.net SQL存儲過程分頁

    asp.net SQL存儲過程分頁

    上周花一下午時間寫了個分頁.給大家分享下,如果寫得不好請大家指出一起討論哈,小弟第一次寫文章哈..謝謝.
    2009-05-05
  • 高效的使用 Response.Redirect解決一些不必要的問題

    高效的使用 Response.Redirect解決一些不必要的問題

    這篇文章主要介紹了如何高效的使用 Response.Redirect解決一些不必要的問題,需要的朋友可以參考下
    2014-05-05

最新評論