0

我创建了一个基于 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
}
4

1 回答 1

0

我通过将点击处理程序替换为 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

于 2012-09-24T15:38:29.980 回答