什么是模拟使用MouseUp& MouseDown事件或其他? [英] What is the best way to simulate a Click with MouseUp & MouseDown events or otherwise?

查看:242
本文介绍了什么是模拟使用MouseUp& MouseDown事件或其他?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF中,大多数控件都有 MouseUp MouseDown 事件(和鼠标按钮特定的变体)一个简单的点击事件,可以立即使用。如果你想使用这些事件有点击行为,你需要处理我认为有点痛苦。



显然的问题是,你不能简单地省略 MouseDown 事件,因为如果您的点击是在另一个控件上启动,并且通过只处理 MouseUp 你想要的点击会触发,但实际上不应该: MouseUp MouseDown 应该发生在同一个控件。



因此,如果有任何问题,我会对这个一般性问题有更好的解决方案。






注意:有几个好的解决方案,如下面所示,我选择接受Rachel的答案,因为它似乎很受欢迎, d喜欢添加以下注释:



Rachel的按钮答案是相当干净和直接的,但你需要将你的实际控制包装在一个按钮,在某些情况下,你可能不会认为你的控件是一个按钮,只是因为它可以被点击(例如如果它更像一个超链接),你还需要每次都引用一个模板。



Rick Sladkey的行为回答更直接地回答原始问题如何只是模拟点击/使控件可点击,缺点是你需要引用 System.Windows.Interactivity 和像Rachel的解决方案它膨胀Xaml代码相当有点。



我的附加事件回答的优点是在Xaml-Markup方面非常接近正常的点击事件有一个属性,唯一的问题,我看到它是在代码中的事件附件没有干净地封装(如果任何人知道一个修复,请添加一个评论的答案)。


<我将使用 Button 控制并覆盖 Button.Template
 < ControlTemplate x:Key =ContentOnlyTemplateTargetType ={x :Type Button}> 
< ContentPresenter />
< / ControlTemplate>

< Button Template ={StaticResource ContentOnlyTemplate}>
< Label Content =Test/>
< / Button>


In WPF most controls have MouseUp and MouseDown events (and the mouse-button-specific variations) but not a simple Click event that can be used right away. If you want to have a click-like behaviour using those events you need to handle both which i consider to be a bit of a pain.

The obvious problem is that you cannot simply omit the MouseDown event because if your click is started on another control and it is released over the control that only handles MouseUp your supposed click will fire while it really should not: Both MouseUp and MouseDown should occur over the same control.

So i would be interested in a more elegant solution to this general problem if there is any.


Notes: There are several good solutions to this as can be seen below, i chose to accept Rachel's answer because it seems to be well received, but additionally i'd like to add the following annotations:

Rachel's button answer is quite clean and straightforward, but you need to wrap your actual control in a button and in some cases you might not really consider your control to be a button just because it can be clicked (e.g. if it is more like a hyperlink), further you need to reference a template every time.

Rick Sladkey's behaviour answer more directly answers the original question of how to just simulate a click/make a control clickable, the drawback is that you need to reference System.Windows.Interactivity and like Rachel's solution it inflates the Xaml-code quite a bit.

My attached event answer has the advantage of being quite close to a normal click event in terms of Xaml-Markup which can be done with one attribute, the only problem i see with it is that the event-attachment in code is not cleanly encapsulated (if anyone knows a fix for that, please add a comment to the answer).

解决方案

I would use a Button control and overwrite the Button.Template to just show the content directly.

<ControlTemplate x:Key="ContentOnlyTemplate" TargetType="{x:Type Button}">
    <ContentPresenter />
</ControlTemplate>

<Button Template="{StaticResource ContentOnlyTemplate}">
    <Label Content="Test"/>
</Button>

这篇关于什么是模拟使用MouseUp&amp; MouseDown事件或其他?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆