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.
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-88d76f7148c1Button
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With