MouseLeftButtonUp not working

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 MouseLeftButtonUp event does not fire. What for? How can i solve this? Thanks.

+4
source share
3 answers

Probably because Button handles the event already, so it does not filter to the Image control.

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

EDIT
After a quick look at the documentation for this event, I see that the routing strategy is for MouseLeftButtonUp Direct , but the actual main event is the MouseUp event in which there is Bubbling . Thus, in fact, MouseLeftButtonUp should have a de facto bubbling strategy, that is, Image should be able to handle the event before Button .

It seems like something else could go on. If nothing is set in the Image control and the background is zero (other than Color.Transparent ), then WPF will treat the control as "nonexistent" as mouse events go on.

EDIT 2
Hmm ... maybe my initial guess was right after all. According to this topic:
http://social.msdn.microsoft.com/forums/en/wpf/thread/e231919d-9fef-4aa5-9dcb-2c1eb68df25b/#306db880-ed50-45d2-819f-88d76f7148c1
Button handles the MouseUp event. Try using PreviewMouseUp and then check which button was clicked.

EDIT 3
Good ... I think I figured it out. I am sure that the button "captures" the mouse on the MouseDown event. When this happens, other controls will not receive MouseUp or MouseDown events (including preview versions) until the mouse is released.
See: http://books.google.com/books?id=nYl7J7z3KssC&pg=PA146#v=twopage&q&f=true

+7
source

The Click event handles the LeftMouseUp event, as Cyborgx37 said.

You can use the PreviewMouseLeftButtonUp event instead of MouseLeftButtonUp , on a button, as shown below:

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

Your Button template has only an image, I think you do not need this button, you can use it as above so that you can capture the MouseLeftUp event.

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

This method is much simpler, but it depends on what you need.

By the way, sorry for my crappy English, hope this can help you.

+4
source

Try using the PreviewMouseLeftButtonUp event.

0
source

Source: https://habr.com/ru/post/1435069/


All Articles