我已经基于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
}
答案 0 :(得分: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