SilverLight中ImageButton控件制作方法
----------------------------------------------------------------------------------
public partial class ImageButton : UserControl
{
public event EventHandler Click = null;
public ImageButton()
{
InitializeComponent();
this.MouseLeftButtonDown += new MouseButtonEventHandler(ImageButton_MouseLeftButtonDown);
this.MouseLeftButtonUp += new MouseButtonEventHandler(ImageButton_MouseLeftButtonUp);
this.MouseEnter += new MouseEventHandler(ImageButton_MouseEnter);
this.MouseLeave += new MouseEventHandler(ImageButton_MouseLeave);
this.SizeChanged += new SizeChangedEventHandler(ImageButton_SizeChanged);
this.image1.Margin = new Thickness(0,0);
if (this.ImageSourcenormal != null)
{
this.image1.source = this.ImageSourcenormal;
//this.LayoutRoot.Background = this.ImageSourcenormal as DependencyObject;
}
ImageButton_SizeChanged(null,null);
}
private void ImageButton_SizeChanged(object sender,SizeChangedEventArgs e)
{
this.image1.Width = this.Width;
this.image1.Height = this.Height;
}
private void ImageButton_MouseLeave(object sender,MouseEventArgs e)
{
if (this.ImageSourcenormal != null)
{
this.image1.source = this.ImageSourcenormal;
}
}
private void ImageButton_MouseEnter(object sender,MouseEventArgs e)
{
if (this.ImageSourceOverlap != null)
{
this.image1.source = this.ImageSourceOverlap;
}
}
private void ImageButton_MouseLeftButtonDown(object sender,MouseButtonEventArgs e)
{
if (this.ImageSourceDown != null)
{
this.image1.source = this.ImageSourceDown;
}
}
private void ImageButton_MouseLeftButtonUp(object sender,MouseButtonEventArgs e)
{
if (this.ImageSourceOverlap != null)
{
this.image1.source = this.ImageSourceOverlap;
}
//单击事件
if (this.Click != null)
{
this.Click(sender,e);
}
}
//定义正常显示图片源
public ImageSource ImageSourcenormal
{
get { return base.GetValue(ImageSourcenormalProperty) as ImageSource; }
set
{
base.SetValue(ImageSourcenormalProperty,value);
this.image1.source = value;
}
}
public static readonly DependencyProperty ImageSourcenormalProperty = DependencyProperty.Register("ImageSourcenormal",typeof(ImageSource),typeof(ImageButton),new PropertyMetadata(null));
//定义Overlap显示图片源
public ImageSource ImageSourceOverlap
{
get { return base.GetValue(ImageSourceOverlapProperty) as ImageSource; }
set { base.SetValue(ImageSourceOverlapProperty,value); }
}
public static readonly DependencyProperty ImageSourceOverlapProperty = DependencyProperty.Register("ImageSourceOverlap",new PropertyMetadata(null));
//定义Down显示图片源
public ImageSource ImageSourceDown
{
get { return base.GetValue(ImageSourceDownProperty) as ImageSource; }
set { base.SetValue(ImageSourceDownProperty,value); }
}
public static readonly DependencyProperty ImageSourceDownProperty = DependencyProperty.Register("ImageSourceDown",new PropertyMetadata(null));
}
ImageButton.xaml
------------------------------------------------------------
<UserControl x:Class="SlApp.Controls.ImageButton"
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" mc:Ignorable="d" d:DesignHeight="227" d:DesignWidth="321"> <Grid x:Name="LayoutRoot" Height="192" Width="278"> <Image Height="168" HorizontalAlignment="Left" Margin="0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="254" /> <Grid.Background> <ImageBrush /> </Grid.Background> </Grid></UserControl>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。