Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Click Event for WPF Image

I am porting an old WinForms Desktop Application to WPF. The app GUI used WinForm's PictureBox to display images. The old WinForms app also had OnClick event handlers for all the PictureBoxes. Clicking the images actually did something important. Now that I am re-doing the UI in WPF, I found out as per this that the equivalent for WinForm's PictureBox control is WPF's Image. However, when I opened up the properties panel for the WPF Image, there was no click event to be handled, so I couldn't write a click event handler like I had in WinForms.

So, can you please tell me what can be done to achieve the equivalent of WinForm's PictureBox and it's click event in WPF? I want to display images and handle the case each time user clicks the image.

like image 986
The Vivandiere Avatar asked May 11 '15 17:05

The Vivandiere


1 Answers

Just add a MouseDown (or MouseLeftButtonDown as suggested) event to your image like so

<Image x:Name=aPicture Source="mypic.jpg" MouseDown="aPicture_MouseDown"/>
// or
<Image x:Name=aPicture Source="mypic.jpg" MouseLeftButtonDown="aPicture_MouseDown"/>

which should add this to your code behind

private void aPicture_MouseDown(object sender, MouseEventArgs e)
{
   //do something here
}
like image 89
CJK Avatar answered Sep 28 '22 03:09

CJK