SilverLight中ImageButton控件制作方法

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, 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", typeof(ImageSource), typeof(ImageButton), 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", typeof(ImageSource), typeof(ImageButton), 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,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="254" />
        <Grid.Background>
            <ImageBrush />
        </Grid.Background>
    </Grid>
</UserControl>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值