我很高兴使用 ICommand
实现来处理按钮上的单个操作:
<Button Command="{Binding Path=ActionDown}">Press Me</Button>
通过 RelayCommand
实现 ICommand
但是我找不到一种简单的方法来为新闻和发布操作(在SO上和互联网上的其他地方)提供操作。 IE 我想做一些像这样的事情,但我不知道该怎么做:
<Button PressCommand="{Binding Path=ActionDown}" ReleaseCommand="{Binding Path=ActionUp}">Press and Release Me</Button>
处理此类需求的正确 MVVM 方法是什么?
最佳答案
您可以使用System.Windows.Interactivity
中的EventTrigger
来触发事件命令
<Button Content="Press Me">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseLeftButtonDown">
<i:InvokeCommandAction Command="{Binding Path=ActionDown}"/>
</i:EventTrigger>
<i:EventTrigger EventName="PreviewMouseLeftButtonUp">
<i:InvokeCommandAction Command="{Binding Path=ActionUp}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
您需要添加对 System.Windows.Interactivity
的引用并定义命名空间
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
关于c# - 用于 MVVM 风格的按钮按下和释放的 ICommand,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26590898/