本文介绍了如何将按钮的 Hold/DoubleTap 事件绑定到 ViewModel 中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含按钮的视图.我想对 Hold 事件执行一个操作.我怎样才能在 mvvm 中做到这一点?对于 Tap 事件,我可以将其绑定到 Command 属性.是否可以用同样的方式做到这一点?

I have a view containing a button. And i want to perform an action on Hold event. How can i do this in mvvm? For Tap event i can Bind it to a Command property. Is it possible to do this with same way?

推荐答案

我会同意 Braulio 的答案 - 我会使用 MVVM Light,但回到 Silverlight 3 天后,我使用自定义附加属性来实现这一点.有关自定义附加属性的示例,请参见此处:http://umairsaeed.com/2010/04/22/custom-attached-properties-in-silverlight/

I would go with Braulio's answer - MVVM Light is what I would use, but back in the Silverlight 3 days I used custom attached properties to achieve this. See here for an example of custom attached properties: http://umairsaeed.com/2010/04/22/custom-attached-properties-in-silverlight/

您可以为保持事件创建一个自定义附加属性来绑定命令,然后像这样使用它:

You could create a custom attached property for the hold event to bind the command to and then use it like so:

<Border local:MyTextBoxControl.HoldEventCommand="{Binding HoldCommand}"/>

与在您的项目中包含 mvvm light 工具包然后执行此操作相比,这是一项大量工作:

This is a lot of work compared with including the mvvm light toolkit in your project and then doing this:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Hold">
        <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding YourCommand}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>

这篇关于如何将按钮的 Hold/DoubleTap 事件绑定到 ViewModel 中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 21:45