实施图片点击:
- 编写一个扩展System.Windows.Control.Image的类。
- 创建一个RoutedEvent和RoutedEventHandler,方便鼠标点击事件。
- 覆盖OnMouseLeftButtonDown
在我的示例中,我评估了点击次数,因为不知道如何更好地做到这一点
public class ImageHelper : Image
{
public static readonly RoutedEvent MouseLeftButtonClick =
EventManager.RegisterRoutedEvent(
"MouseLeftButtonClick",
RoutingStrategy.Bubble,
typeof(RoutedEventHandler),
typeof(ImageHelper));
public event RoutedEventHandler MouseLeftButtonClickEvent
{
add
{
AddHandler(MouseLeftButtonClick, value);
}
remove
{
RemoveHandler(MouseLeftButtonClick, value);
}
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
if (e.ClickCount == 1)
{
RaiseEvent(new MouseLeftButtonClickEventArgs(
MouseLeftButtonClick, this));
}
base.OnMouseLeftButtonDown(e);
}
public class MouseLeftButtonClickEventArgs : RoutedEventArgs
{
public MouseLeftButtonClickEventArgs(RoutedEvent routedEvent, object source)
: base(routedEvent, source)
{
// some code.....
}
}
}
XAML:
<local:ImageHelper>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonClickEvent">
<i:InvokeCommandAction Command="{Binding Path=MyCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</local:ImageHelper>