如何触发视图模型命令为特定的按钮事件 [英] How to trigger ViewModel command for a specific button events

查看:106
本文介绍了如何触发视图模型命令为特定的按钮事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何能在一个视图模型的命令通过按钮的一个特定的事件,如 MouseDoubleClick

调用
解决方案

您可以在 System.Windows.Interactivity 的命名空间中使用的EventTrigger ,这就是所谓的棱镜框架的一部分。如果你刚开始接触MVVM,不要现在太在意了棱镜,但是记住它供以后使用。无论如何,你可以钢材的EventTrigger

它的工作原理是这样的:

引用的程序集的 System.Windows.Interactivity.dll

在XAML中,引用命名空间:

<$p$p><$c$c>xmlns:i=\"clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity\"

然后在你的按钮或其他控制,加的EventTrigger是这样的:

 &LT;按钮内容=按钮&GT;
   &LT; I:Interaction.Triggers&GT;
      &LT; I:的EventTrigger事件名称=MouseDoubleClick&GT;
         &LT; I:InvokeCommandAction命令={结合CommandToBindTo}
                                CommandParameter ={结合CommandParameterToBindTo}/&GT;
      &LT; /我:&的EventTrigger GT;
   &LT; /我:Interaction.Triggers&GT;
&LT; /按钮&GT;

这样,你在你的DataContext你的事件绑定到的命令

备注

要明确使用,这里是一种活生生的例子包括视图模型的。虚构的要求是,让用户在列表中选择一个项目,然后执行命令这需要选择的项目作为一个参数:

 &LT; ListBox的X:名称=ItemsList的ItemsSource ={绑定表项}/&GT;&LT;按钮内容=你有选择的项目的东西&GT;
   &LT; I:Interaction.Triggers&GT;
      &LT; I:的EventTrigger事件名称=MouseDoubleClick&GT;
         &LT; I:InvokeCommandAction命令={结合DoSomethingCommand}
                                CommandParameter ={绑定的SelectedItem,
                                                   的ElementName = ItemsList}/&GT;
      &LT; /我:&的EventTrigger GT;
   &LT; /我:Interaction.Triggers&GT;
&LT; /按钮&GT;

和这将是视图模型。注意如何将参数的命令后,在本例中使用 DelegateCommand 对象的一个​​通用版本,你在每一个MVVM框架(得到它有时 RelayCommand )。这个类接收所需的参数的类型作为泛型参数(这里 ItemViewModel ),并要求它接受一个根据参数(这里的方法 ExecuteDoSomethingWithItem(ItemViewModel ...))。其余的是WPF魔法:oject到的 CommandParameter 属性在XAML绑定将通过作为参数传递你的执行(.. 。)功能。

 公共类视图模型
{
    的ObservableCollection&LT; ItemViewModel&GT;项目{搞定;组; }    公众的ICommand DoSomethingCommand
    {
        得到
        {
            返回_doSomethingCommand?
                   (_doSomethingCommand =新DelegateCommand&LT; ItemViewModel&GT;(ExecuteDoSomethingWithItem));
        }
    }    私人DelegateCommand&LT; ItemViewModel&GT; _doSomethingCommand;    私人无效ExecuteDoSomethingWithItem(ItemViewModel itemToDoSomethingWith)
    {
        // 做一点事
    }    公共视图模型()
    {
        项目=新的ObservableCollection&LT; ItemViewModel&GT;();
        //填充集合
    }
}

有乐趣的学习MVVM,这是值得的。

How can a command on a ViewModel be invoked by a specific event of a button, such as MouseDoubleClick?

解决方案

You can use the EventTrigger in the System.Windows.Interactivity namespace, which is part of the so-called Prism framework. If you're just getting started with MVVM, don't care too much for Prism by now, but keep it in mind for later. Anyway, you can steel the EventTrigger

It works like this:

Reference the assembly System.Windows.Interactivity.dll

In XAML, reference the namespace:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

Then in your Button or any other control, add a EventTrigger like this:

<Button Content="Button">
   <i:Interaction.Triggers>
      <i:EventTrigger EventName="MouseDoubleClick">
         <i:InvokeCommandAction Command="{Binding CommandToBindTo}" 
                                CommandParameter="{Binding CommandParameterToBindTo}" />
      </i:EventTrigger>
   </i:Interaction.Triggers>
</Button>

This way, you bind your event to a Command on your DataContext.

Remark

To clarify the usage, here's a kind of real life example including the ViewModel. The fictional requirement is to allow the user to select an item in a list and then perform a command which takes the selected item as a parameter:

<ListBox x:Name="ItemsList" ItemsSource="{Binding Items}" />

<Button Content="Do something with selected item">
   <i:Interaction.Triggers>
      <i:EventTrigger EventName="MouseDoubleClick">
         <i:InvokeCommandAction Command="{Binding DoSomethingCommand}" 
                                CommandParameter="{Binding SelectedItem, 
                                                   ElementName=ItemsList}" />
      </i:EventTrigger>
   </i:Interaction.Triggers>
</Button>

And that would be the ViewModel. Note how the parameter to the command is used, in the example with a generic version of a DelegateCommand object as you get it in every MVVM framework (sometimes RelayCommand). This class takes the type of the required parameter as a generic parameter (here ItemViewModel) and requires a method which takes an according parameter (here ExecuteDoSomethingWithItem(ItemViewModel ...)). The rest is WPF magic: The oject to which the CommandParameter property is bound in your XAML will be passed through as the parameter in your Execute(...) function.

public class ViewModel
{
    ObservableCollection<ItemViewModel> Items { get; set; }

    public ICommand DoSomethingCommand
    {
        get
        {
            return _doSomethingCommand ??
                   (_doSomethingCommand = new DelegateCommand<ItemViewModel>(ExecuteDoSomethingWithItem));
        }
    }

    private DelegateCommand<ItemViewModel> _doSomethingCommand;

    private void ExecuteDoSomethingWithItem(ItemViewModel itemToDoSomethingWith)
    {
        // Do something
    }

    public ViewModel()
    {
        Items = new ObservableCollection<ItemViewModel>();
        // Fill the collection
    }
}

Have fun with learning MVVM, it's worth it.

这篇关于如何触发视图模型命令为特定的按钮事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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