MouseLeftButtonUp does not fire

2019-01-20 11:29发布

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.

3条回答
做个烂人
2楼-- · 2019-01-20 11:44

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

查看更多
Summer. ? 凉城
3楼-- · 2019-01-20 11:56

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.

查看更多
Melony?
4楼-- · 2019-01-20 12:11

Try using the event PreviewMouseLeftButtonUp.

查看更多
登录 后发表回答