Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
534 views
in Technique[技术] by (71.8m points)

c# - MouseLeftButtonUp does not fire

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...