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>