图片点击事件
方法1:
界面:
<Window x:Class="ImageClick.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Image Height="227" HorizontalAlignment="Left" Margin="92,42,0,0" Name="imageTest1" Stretch="Fill" VerticalAlignment="Top" Width="203" />
<!--<Image Height="227" HorizontalAlignment="Left" Margin="92,42,0,0" Name="imageTest2" Stretch="Fill" VerticalAlignment="Top" Width="203" Source="/ImageClick;component/IMG/imgTest.png" />-->
</Grid>
</Window>
逻辑代码:
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Imaging;
namespace ImageClick
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public event MouseButtonEventHandler imageMouseUp;
#if false
#else
public MainWindow()
{
InitializeComponent();
BitmapImage bi = new BitmapImage();
// BitmapImage.UriSource must be in a BeginInit/EndInit block.
bi.BeginInit();
bi.UriSource = new Uri(@"IMG/imgTest.png", UriKind.RelativeOrAbsolute);
bi.EndInit();
imageTest1.Source = bi;
EmulateClickEvent(imageTest1, ImageOnClick);
}
/// <summary>
/// 为控件附加模拟鼠标单击事件
/// </summary>
/// <param name="control">需要附加事件的控件</param>
/// <param name="handler">鼠标单击事件的处理函数</param>
/// <remarks>
/// 在控件上MouseDown->MouseLeave->MouseEnter->MouseUp同样有效
/// </remarks>
private bool EmulateClickEvent(FrameworkElement control, MouseButtonEventHandler handler)
{
if (control == null || handler == null) return false;
int status = 0;
control.MouseEnter += delegate { if (status == 0) status = -1; };
control.MouseLeave += delegate { status = 0; };
control.MouseLeftButtonDown += delegate { status = 1; };
control.MouseLeftButtonUp += delegate(object sender, MouseButtonEventArgs e) { if (status > 0) handler(sender, e); status = 0; };
return true;
}
void ImageOnClick(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("Method1!");
}
#endif
}
}
方法2
界面:
<Window x:Class="ImageClick.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Image Height="227" HorizontalAlignment="Left" Margin="92,42,0,0" Name="imageTest1" Stretch="Fill" VerticalAlignment="Top" Width="203" MouseLeftButtonUp="imageTest1_MouseLeftButtonUp" />
<!--<Image Height="227" HorizontalAlignment="Left" Margin="92,42,0,0" Name="imageTest2" Stretch="Fill" VerticalAlignment="Top" Width="203" Source="/ImageClick;component/IMG/imgTest.png" />-->
</Grid>
</Window>
逻辑代码:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
namespace ImageClick
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public event MouseButtonEventHandler imageMouseUp;
#if true
public MainWindow()
{
InitializeComponent();
BitmapImage bi = new BitmapImage();
// BitmapImage.UriSource must be in a BeginInit/EndInit block.
bi.BeginInit();
bi.UriSource = new Uri(@"IMG/imgTest.png", UriKind.RelativeOrAbsolute);
bi.EndInit();
imageTest1.Source = bi;
imageMouseUp += new MouseButtonEventHandler(imageTest1_MouseLeftButtonUp);
//imageTest1.AddHandler(Image.MouseDownEvent, this.imageMouseDown);
imageTest1.AddHandler(Image.MouseLeftButtonUpEvent, imageMouseUp, true);
}
private void imageTest1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("Method2!");
}
#else
#endif
}
}