简体   繁体   English

如何将按钮的Hold / DoubleTap事件绑定到ViewModel中的属性

[英]How can I bind a Hold/DoubleTap event of a button to a property in ViewModel

I have a view containing a button. 我有一个包含按钮的视图。 And i want to perform an action on Hold event. 我想在Hold事件上执行操作。 How can i do this in mvvm? 我如何在mvvm中做到这一点? For Tap event i can Bind it to a Command property. 对于Tap事件,我可以将其绑定到Command属性。 Is it possible to do this with same way? 是否可以用相同的方式做到这一点?

不知道它是否支持该命令,否则,您可以使用MVVM Light Toolkit(免费和开源)行为:EventToCommand

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. 我会使用Braulio的答案-MVVM Light是我要使用的,但是回到Silverlight 3天后,我使用自定义附加属性来实现这一目标。 See here for an example of custom attached properties: http://umairsaeed.com/2010/04/22/custom-attached-properties-in-silverlight/ 有关自定义附加属性的示例,请参见此处: 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: 您可以为hold事件创建一个自定义附加属性,将命令绑定到该属性,然后像这样使用它:

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

This is a lot of work compared with including the mvvm light toolkit in your project and then doing this: 与在项目中包含mvvm light工具箱然后执行以下操作相比,这需要大量工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将WPF DataGridComboBoxColumn绑定到ViewModel属性? - How can I bind WPF DataGridComboBoxColumn to ViewModel Property? 如何将前景绑定到ViewModel中的属性? - How can I bind Foreground to a property in my ViewModel? 如何将模型绑定到ViewModel中存在的属性? - How can I Model Bind to a property that exists in my ViewModel? 如何将listviewitem中的按钮绑定到Winrt中的ViewModel中的Command - How can i bind a button in listviewitem to a Command in ViewModel in Winrt 如何将“多边形填充”属性绑定到按钮的“背景”属性? - How can I bind a Polygon Fill property to the Background property of the button? 我可以将事件绑定到按钮吗? - can I bind event to a button? 如何直接绑定到子视图模型? - How can I bind directly to a child viewmodel? 如何将此View绑定到此ViewModel? - How can I bind this View to this ViewModel? MVVM,当 ItemsSource 为 ObservableCollection 时,您能否将 ViewModel 中的命令绑定到 ItemsRepeater 中的 Button<model> 和 ViewModel 的属性?</model> - MVVM, Can you bind command in ViewModel to Button in ItemsRepeater when ItemsSource is ObservableCollection<Model> and a property of the ViewModel? 如何绑定到 ViewModel 的属性和 ViewModel 中集合项的属性? - How can I bind to properties of the ViewModel and properties of collection items in the ViewModel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM