首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用类型按钮作为基础的CustomControl不触发MouseLeftButtonUp事件

使用类型按钮作为基础的CustomControl不触发MouseLeftButtonUp事件
EN

Stack Overflow用户
提问于 2012-09-24 20:15:56
回答 1查看 215关注 0票数 0

我已经基于Button控件创建了一个简单的自定义ImageButton控件。我的想法是提供一个鼠标按下的图像和一个鼠标向上的图像,图像根据鼠标左键的操作进行交换。MouseLeftButtonDown事件触发正常,我的图像被更新,但MouseLeftButtonUp事件从不触发。

这是有原因的吗,是我没有正确地实现它吗?

自定义控件:

代码语言:javascript
复制
namespace Reader.Controls
{
    using System.IO;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Media.Imaging;

    [TemplatePart(Name = "PART_Image", Type = typeof(Image))]
    public class ImageButton : Button
    {
        public Image PartImage;

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

        public override void OnApplyTemplate()
        {
            this.PartImage = this.GetTemplateChild("PART_Image") as Image;

            if (this.PartImage != null)
            {
                this.PartImage.MouseLeftButtonDown += this.PartImageOnMouseLeftButtonDown;
                this.PartImage.MouseLeftButtonUp += this.PartImageOnMouseLeftButtonUp;

                this.SetImageSource(OffImageSource);
            }
        }

        #region Dependency properties

        public byte[] OffImageSource
        {
            get
            {
                return (byte[])this.GetValue(OffImageProperty);
            }
            set
            {
                this.SetValue(OffImageProperty, value);
            }
        }

        public static DependencyProperty OffImageProperty = DependencyProperty.Register(
            "OffImageSource", typeof(byte[]), typeof(ImageButton), new PropertyMetadata(null));

        public byte[] OnImageSource
        {
            get
            {
                return (byte[])this.GetValue(OnImageProperty);
            }
            set
            {
                this.SetValue(OnImageProperty, value);
            }
        }

        public static DependencyProperty OnImageProperty = DependencyProperty.Register(
            "OnImageSource", typeof(byte[]), typeof(ImageButton), new PropertyMetadata(null));

        #endregion

        #region Button events

        private void PartImageOnMouseLeftButtonDown(object sender, MouseButtonEventArgs mouseButtonEventArgs)
        {
            if (this.PartImage != null)
            {
                this.SetImageSource(OnImageSource);
            }
        }

        private void PartImageOnMouseLeftButtonUp(object sender, MouseButtonEventArgs mouseButtonEventArgs)
        {
            if (this.PartImage != null)
            {
                this.SetImageSource(OffImageSource);
            }
        }

        #endregion

        private void SetImageSource(byte[] imageSource)
        {
            using (var ms = new MemoryStream(imageSource, 0, imageSource.Length - 1))
            {
                var bi = new BitmapImage();
                bi.SetSource(ms);

                this.PartImage.Source = bi;
            }
        }
    }
}

默认样式模板:

代码语言:javascript
复制
<Style TargetType="Controls:ImageButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Controls:ImageButton">
                <Image x:Name="PART_Image" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

用法:

代码语言:javascript
复制
<Controls:ImageButton x:Name="btnLangEn" Click="btnLangEn_Click" />

使用代码(ImageLibrary是对资源文件的引用):

代码语言:javascript
复制
public LanguageSelection()
{
    InitializeComponent();

    btnLangEn.OffImageSource = ImageLibrary.LangEn_off;
    btnLangEn.OnImageSource = ImageLibrary.LangEn_on;
}

private void btnLangEn_Click(object sender, RoutedEventArgs e)
{
    // handle click event here
}
EN

回答 1

Stack Overflow用户

发布于 2012-09-24 23:38:30

我对此进行了排序,将点击处理程序替换为OnMouseLeftButtonDown和OnMouseLeftButtonUp的重写方法,如下所示:

代码语言:javascript
复制
    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        base.OnMouseLeftButtonDown(e);

        if (this.PartImage != null)
        {
            this.SetImageSource(OnImageSource);
        }
    }

    protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
    {
        base.OnMouseLeftButtonUp(e);

        if (this.PartImage != null)
        {
            this.SetImageSource(OffImageSource);
        }
    }

在派生类中处理事件时,建议使用以下方式:http://msdn.microsoft.com/en-us/library/system.windows.controls.control.onmouseleftbuttonup(v=vs.95).aspx

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12564878

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档