5

I have a Button

<Button>
    <Button.Template>
        <ControlTemplate>
            <StackPanel>
                <Image Source="share.png" 
                       MouseLeftButtonUp="Image_MouseLeftButtonUp"
                       MouseLeftButtonDown="Image_MouseLeftButtonDown" />
            </StackPanel>
        </ControlTemplate>
    </Button.Template>
</Button>

But the problem is that, unlike MouseLeftButtonDown, the event MouseLeftButtonUp does not fire. Why? How can I solve it? Thanks.

Nick
  • 10,309
  • 21
  • 97
  • 201

3 Answers3

9

Probably because the Button is handling the event already, so it doesn't filter down to the Image control.

Why are you handling the MoustUp and MouseDown events on the Image? Why not handle them on the Button?

EDIT
After a quick look at the documentation for this event, I see that the routing strategy for MouseLeftButtonUp is Direct, but the actual underlying event is the MouseUp event which has a Bubbling strategy. So, in effect, MouseLeftButtonUp should have a de-facto bubbling strategy, meaning the Image should have the opportunity to handle the event before the Button.

Sounds like something else might be going on. If you set the Image to nothing on the Image control, and the background is null (different from Color.Transparent), then WPF will treat the control as not "existing" as far as mouse events go.

EDIT 2
Hmm... maybe my original guess was correct afterall. According to this thread:
http://social.msdn.microsoft.com/forums/en/wpf/thread/e231919d-9fef-4aa5-9dcb-2c1eb68df25b/#306db880-ed50-45d2-819f-88d76f7148c1
Button is handling the MouseUp event. Try using PreviewMouseUp and then checking to see which button was clicked.

EDIT 3
Ok... I think I figured it out. I'm betting that the Button is "capturing" the Mouse on the MouseDown event. When this occurs, other controls will not receive MouseUp or MouseDown events (include the Preview versions) until the Mouse has been released.
See: http://books.google.com/books?id=nYl7J7z3KssC&pg=PA146#v=twopage&q&f=true

JDB
  • 25,172
  • 5
  • 72
  • 123
  • I have to change the Image Source using these events. Even using Button event MouseLeftButtonUp it does not work. – Nick Sep 19 '12 at 14:05
  • I believe that the `MouseLeftButtonUp` event is only called after `MouseUp`. Since the `Button` is probably setting the `IsHandled` property to `true` for `MouseUp`, `MouseLeftButtonUp` is probably never being raised. Trying handling `MouseUp` on the `Button`. (You may even need to handle `PreviewMouseUp` - not sure about the order the events are handled.) – JDB Sep 19 '12 at 15:28
5

The Click event is handling the LeftMouseUp event as Cyborgx37 said.

You can use the PreviewMouseLeftButtonUp event instead of MouseLeftButtonUp, on Button as you can see below:

<Button PreviewMouseLeftButtonUp="Button_PreviewMouseLeftButtonUp">
    <Button.Template>
        <ControlTemplate>
            <StackPanel>
                <Image Source="..." />
            </StackPanel>
        </ControlTemplate>
    </Button.Template>
</Button>

Your Button template only has an Image, I guess you don't need this Button, you can use as above, so you can capture the MouseLeftUp event.

<Image MouseLeftButtonUp="Image_MouseLeftButtonUp" Source="..." />

This way is so much easier, but it depends on what you need.

Btw, sorry for my crap english, I hope it could help you.

Wiley Marques
  • 485
  • 3
  • 16
0

Try using the event PreviewMouseLeftButtonUp.

Raúl Otaño
  • 4,640
  • 3
  • 31
  • 65