【问题标题】:CustomControl using type Button as base not firing MouseLeftButtonUp eventCustomControl 使用 Button 类型作为基础不触发 MouseLeftButtonUp 事件
【发布时间】:2012-09-24 23:38:29
【问题描述】:

我创建了一个基于 Button 控件的简单自定义 ImageButton 控件。这个想法是我提供了一个鼠标按下图像和一个鼠标按下图像,并且图像根据鼠标左键操作进行交换。 MouseLeftButtonDown 事件触发得很好,我的图像已更新,但 MouseLeftButtonUp 事件永远不会触发。

这是有原因的吗,是我执行不正确吗?

自定义控件:

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;
            }
        }
    }
}

默认样式模板:

<Style TargetType="Controls:ImageButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Controls:ImageButton">
                <Image x:Name="PART_Image" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

用法:

<Controls:ImageButton x:Name="btnLangEn" Click="btnLangEn_Click" />

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

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
}

【问题讨论】:

    标签: silverlight silverlight-5.0


    【解决方案1】:

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

        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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-11
      • 1970-01-01
      • 1970-01-01
      • 2010-09-24
      • 1970-01-01
      • 2011-04-05
      • 2013-03-12
      相关资源
      最近更新 更多