Question

I'm creating an application (for a tablet PC) where a user will fill out an inspection form. As a final step, the director must be able to sign it (along with a few other people).

Since I want to keep my form small and concise, I was looking to create a click event on an image control which would pop up my signature canvas window.

XAML Code for my Image control

...
<DockPanel HorizontalAlignment="Stretch" Name="dpDirectorImg" VerticalAlignment="Stretch" Grid.Column="1" Grid.Row="8">
   <Image Height="Auto" Name="imgDirectorSignature" Stretch="Uniform" Width="Auto" MouseDown="imgDirectorSignature_MouseDown" />             
</DockPanel>
...

VB Code

Private Sub imgDirectorSignature_MouseDown(sender As Object, e As System.Windows.Input.MouseButtonEventArgs) Handles imgDirectorSignature.MouseDown
    MsgBox("Hello World")
End Sub

For example, in the following screenshot the user would touch / click in the red rectangle (which contains an Image control):

Signature location

Which would prompt this window:

Signature window

My problem: I cannot seem to trigger the event on the Image. I have tried TouchDown, MouseDown, StylusDown, GotFocus (with property Focasable set to true) and nothing seems to trigger it.

Was it helpful?

Solution

Well now that we found out what was the problem.

How about you stop using empty images for the future and instead you use Rectangle:

<DockPanel HorizontalAlignment="Stretch" Name="dpDirectorImg" VerticalAlignment="Stretch" Grid.Column="1" Grid.Row="8">
   <Rectangle MouseDown="imgDirectorSignature_MouseDown" />             
</DockPanel>

If the rectangle has any background color by default you can change it to transparent

Background = Transparent

Glad I was able to help you out on this and notice, when Background or Source of an Image is NULL, the hit testing in wpf will not work. That is by design.

Set Background to transparent or any color you like :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top