silverlight ImageButton

using System;using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;using System.Windows.Ink;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using System.ComponentModel; namespace AHMSL.SL.Controls{    public class ImageButton : Button    {        public ImageButton()            : base()        {            this.DefaultStyleKey = typeof(ImageButton);        }         public override void OnApplyTemplate()        {            base.OnApplyTemplate();        }         public static readonly DependencyProperty normalImageProperty = DependencyProperty.Register("NormalImage"typeof(ImageSource), typeof(ImageButton), null);        public ImageSource NormalImage        {            get { return (ImageSource)GetValue(normalImageProperty); }            set { SetValue(normalImageProperty, value); }        }         public static readonly DependencyProperty hoverImageProperty = DependencyProperty.Register("HoverImage"typeof(ImageSource), typeof(ImageButton), null);        public ImageSource HoverImage        {            get { return (ImageSource)GetValue(normalImageProperty); }            set { SetValue(hoverImageProperty, value); }        }         public static readonly DependencyProperty clickImageProperty = DependencyProperty.Register("ClickImage"typeof(ImageSource), typeof(ImageButton), null);        public ImageSource ClickImage        {            get { return (ImageSource)GetValue(normalImageProperty); }            set { SetValue(clickImageProperty, value); }        }         public static readonly DependencyProperty DisableImageProperty = DependencyProperty.Register("DisableImage"typeof(ImageSource), typeof(ImageButton), null);        public ImageSource DisableImage        {            get { return (ImageSource)GetValue(normalImageProperty); }            set { SetValue(DisableImageProperty, value); }        }    }}

 

 

generic.xaml

 <Style TargetType="local:ImageButton">
        <!-- key lines to enable the control -->
        <Setter Property="IsEnabled" Value="true" />
        <Setter Property="IsTabStop" Value="true" />
        <Setter Property="Cursor" Value="Hand" />
 
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:ImageButton">
                    <Grid x:Name="Root" Margin="0" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
                        <Image x:Name="disableBlock" Source="{TemplateBinding DisableImage}" Opacity="0" Stretch="None"/>
                        <Image x:Name="normalBlock" Source="{TemplateBinding NormalImage}" Stretch="None"/>
                        <Image x:Name="hoverBlock" Source="{TemplateBinding HoverImage}" Opacity="0" Stretch="None"/>
                        <Image x:Name="clickBlock" Source="{TemplateBinding ClickImage}" Opacity="0" Stretch="None"/>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="FocusStates">
                                <VisualState x:Name="Unfocused"/>
                                <VisualState x:Name="Focused"/>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition GeneratedDuration="00:00:0.5" To="MouseOver"/>
                                    <VisualTransition GeneratedDuration="00:00:0.1" To="Pressed"/>
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="normalBlock" Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:0.3"/>
                                        <DoubleAnimation Storyboard.TargetName="hoverBlock" Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0.2"/>
                                        <DoubleAnimation Storyboard.TargetName="disableBlock" Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:0.3"/>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="normalBlock" Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:0.3"/>
                                        <DoubleAnimation Storyboard.TargetName="hoverBlock" Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:0.3"/>
                                        <DoubleAnimation Storyboard.TargetName="clickBlock" Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0.3"/>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="normalBlock" Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:0.3"/>
                                        <DoubleAnimation Storyboard.TargetName="hoverBlock" Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:0.3"/>
                                        <DoubleAnimation Storyboard.TargetName="disableBlock" Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0.3"/>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Normal" />
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值