wpf MouseLeftButtonUp 不触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12496258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:25:45  来源:igfitidea点击:

MouseLeftButtonUp does not fire

c#wpfeventsbuttonmouseevent

提问by Nick

I have a Button

我有一个 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.

但问题是,与 MouseLeftButtonDown 不同的是,事件 MouseLeftButtonUp 不会触发。为什么?我该如何解决?谢谢。

回答by JDB still remembers Monica

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

可能是因为Button它已经在处理事件,所以它不会过滤到 Image 控件。

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

你为什么要处理 MoustUp 和 MouseDown 事件Image?为什么不处理它们Button呢?

EDIT
After a quick look at the documentation for this event, I see that the routing strategy for MouseLeftButtonUpis Direct, but the actual underlying event is the MouseUpevent which has a Bubblingstrategy. So, in effect, MouseLeftButtonUpshould have a de-facto bubbling strategy, meaning the Imageshould have the opportunity to handle the event before the Button.

编辑
快速查看此事件的文档后,我看到MouseLeftButtonUp的路由策略是Direct,但实际的基础事件是MouseUp事件,它具有冒泡策略。所以,实际上,MouseLeftButtonUp应该有一个事实上的冒泡策略,这意味着Image应该有机会在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.

听起来可能还有其他事情发生。如果您在 Image 控件上将 Image 设置为空,并且背景为空(不同于Color.Transparent),则 WPF 会将控件视为不“存在”,就鼠标事件而言。

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
Buttonis handling the MouseUpevent. Try using PreviewMouseUpand then checking to see which button was clicked.

编辑 2
嗯……也许我最初的猜测是正确的。根据这个线程:
http: //social.msdn.microsoft.com/forums/en/wpf/thread/e231919d-9fef-4aa5-9dcb-2c1eb68df25b/#306db880-ed50-45d2-819f-88d76f7148c1
Button正在处理MouseUp事件 尝试使用PreviewMouseUp,然后检查点击了哪个按钮。

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

编辑 3
好的...我想我想通了。我打赌 Button 正在“捕获” MouseDown 事件上的鼠标。发生这种情况时,其他控件将不会收到 MouseUp 或 MouseDown 事件(包括预览版本),直到鼠标被释放。
请参阅:http: //books.google.com/books?id=nYl7J7z3KssC&pg=PA146#v=twopage&q&f=true

回答by Wiley Marques

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

正如 Cyborgx37 所说,Click 事件正在处理 LeftMouseUp 事件。

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

您可以在 Button 上使用PreviewMouseLeftButtonUp事件而不是MouseLeftButtonUp, 如下所示:

<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.

你的 Button 模板只有一个 Image,我猜你不需要这个 Button,你可以像上面那样使用,这样你就可以捕获 MouseLeftUp 事件。

<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.

顺便说一句,对不起我的垃圾英语,我希望它可以帮助你。

回答by Raúl Ota?o

Try using the event PreviewMouseLeftButtonUp.

尝试使用事件 PreviewMouseLeftButtonUp。