c# - 用于 MVVM 风格的按钮按下和释放的 ICommand

标签 c# wpf mvvm

我很高兴使用 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/

相关文章:

c# - 即使为 Assembly 设置 MaxMessageSize 后,Fabric Message 仍然太大

c# - 为什么使用隐式转换运算符的自定义结构上的 Assert.AreEqual 会失败?

c# - 删除边框 winforms 和 WindowState Maximized without fullscreen

c# - 如何使用 iCommand 在 MainWindow 中更改我的 Textblock 中的文本?

Android MVVM设计模式( Activity 间通信)

c# - MSBuild:如何在链接的父目录中包含一堆文件?

c# - 为什么 ListBox 的 DesiredSize.Width 始终为零?

c# - 背景颜色阻碍 WPF UserControl 内容

c# - 如何在 XAML 中设置背景不透明度和边框不透明度?

WPF Datagrid selecteditem = MVVM 中的 null