0

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.

Alex
  • 4,821
  • 16
  • 65
  • 106
  • Place your Image in a Button. Set override Buttons default style property to true so you dont have those button alike animations and button alike borders. And you will be able to listen to Button.Click event. Or you can use Buttons Command :) – dev hedgehog Oct 28 '13 at 18:38
  • @devhedgehog I see... I was using an image because in the futur, I was wanting to portray the written signature in the image control once the user had accepted it ... I guess I could just put it as the background of the button anyway and it would work? – Alex Oct 28 '13 at 18:50
  • That with Buttons was a suggestion. It also works when you listen to MouseDown event on Image control. Every control inherits from UIElement and UIElement defines events like MouseDown, MouseLeftButtonDown, MouseMove,... etc. Therefore every control has those things. Now to your case. I dont see anywhere that you listening to mouse events on your Image control. – dev hedgehog Oct 28 '13 at 18:53
  • @devhedgehog I modified the code and I still can't get it to trigger, I must be doing something totally off... I'm new to WPF – Alex Oct 28 '13 at 19:31
  • Ahhh, I just took a better look on your question. Remove OpacityMask property. If opacity is set to NULL value, hit testing (clicking somewhere with mouse around) is not working in WPF. – dev hedgehog Oct 28 '13 at 19:38
  • @devhedgehog Tried to remove it and still can't get it to work ... This is weird. – Alex Oct 28 '13 at 19:51
  • Dammit, let me look again on your code. :) Have you set the Source property? Do you even have an PNG image or is that just an empty image control? – dev hedgehog Oct 28 '13 at 19:52
  • @devhedgehog It is an empty image control, no source has been set. – Alex Oct 28 '13 at 19:54
  • @devhedgehog I see. I just put a bogus image and it worked ... Is there a way to do this without having an image source? – Alex Oct 28 '13 at 19:56
  • 1
    Let me write you an answer so you can mark it and I get some of those tiny points ahahha – dev hedgehog Oct 28 '13 at 19:58

1 Answers1

1

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 :)

dev hedgehog
  • 8,698
  • 3
  • 28
  • 55
  • Just a heads up, using this method (a rectangle) I had to set the `Fill` attribute to White... If I set it to none, it will not trigger the event. Also, I couldn't find any `Background` attribute to the rectnagle. – Alex Oct 28 '13 at 20:07
  • That stupid rectangle sucks. I hate coping code from internet and then it doesnt have the properties as described haha. Leave Rectable away and just have your DockPanel catching events. You can assign MouseDown at DockPanel level. – dev hedgehog Oct 28 '13 at 20:08