0

I made a usercontrol in WPF with an image in it. I declared a MouseDown event for this image:

<Image x:Name="imgState" Height="300" Width="300" MouseDown="imgState_MouseDown" OpacityMask="#00000000" />

I placed this usercontrol on my application form, but the event isn't fireing. I'm pretty new to WPF and I read about RoutedEvents but I don't really understand it. I would be happy if someone could help and explain this to me!

Update

Changing to PreviewMouseDown didn't fire the event too. I tried setting the background to transparent and even tried with a blank 300x300 image. The grid workaround doesn't fire the event too. Here is how my code behind looks like:

private void imgState_MouseDown(object sender, MouseButtonEventArgs e)
{
//Some code here
}

Update 2

Here is my whole XAML file:

<UserControl x:Class="TicTacToe.controls.SingleField"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Image x:Name="imgState" MouseDown="imgState_MouseDown"  Height="300" Width="300" Stretch="None" OpacityMask="#00000000"/>

    </Grid>
</UserControl>

I removed the source again because I set one from code behind at runtime and adding a transparent/clear image didn't helped.

Kimmax
  • 1,658
  • 21
  • 34

3 Answers3

1

You probably want PreviewMouseUp instead of MouseDown event

<Image x:Name="imgState" Height="300" Width="300" 
 PreviewMouseUp="ImgState_OnPreviewMouseUp" 
 PreviewMouseDown="ImgState_OnPreviewMouseDown"/>

Either of the two, you can capture the event from there.

123 456 789 0
  • 10,565
  • 4
  • 43
  • 72
  • Please see my update :) Could you explain me the drifference between **PreviewMouseDown** and **MouseDown**? MSDN says the **PreviewMouseDown** is fireing when a mouse button is pressed and is above a control, **MouseDown** fires when a mouse button is pressed (Shouldn't this event only fire when the mouse is above the control too?) – Kimmax Dec 20 '13 at 11:42
  • Show me your entire XAML. Where is this image sitting? What is its parent? – 123 456 789 0 Dec 20 '13 at 16:43
  • Added that, parent is a `Grid` – Kimmax Dec 20 '13 at 22:23
  • Does the image has a source? If it doesn't have anything to show then yes no events will be fired. – 123 456 789 0 Dec 21 '13 at 18:14
  • As I said, I tried with transparent background color, transparent 300x300 image and a white 300x300 image. Should I upload the project, so you can have a look in it? It's a VS2013 project – Kimmax Dec 21 '13 at 19:26
  • Thank you, but I solved the problem myself, see my answer if you want to know the solution – Kimmax Dec 23 '13 at 17:40
0

If the answer above does not help:

Not a very nice solution but does work so many times:

Wrap your image with a grid on which you will have your event...

<Grid MouseDown="imgState_MouseDown">
<Image/>
</Grid>
florianbaer
  • 170
  • 1
  • 11
0

Okay I solved the problem myself.

The problem was the setting OpacityMask="#00000000" that prevented the image from appearing so there were, as @lll said, nothing to hit. I don't know when the setting was set, but I think it happened automatically while expanding the Representation tab.

Thanks for helping me!

Community
  • 1
  • 1
Kimmax
  • 1,658
  • 21
  • 34