Mvvm-Light Silverlight,将EventToCommand与组合框配合使用 [英] Mvvm-Light Silverlight, using EventToCommand with a Combobox

查看:119
本文介绍了Mvvm-Light Silverlight,将EventToCommand与组合框配合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将ComboBox的SelectedItemChangeEvent连接到了视图模型中的ICommand.一切似乎都正常,但是我不知道如何获取ComboxBox的SelectedItem.我想我需要使用EventToCommand的CommandParameter-是否将此绑定到具有ComboBox的selectedItem的ViewModel中?我已经尝试过了:

I've hooked up a ComboBox's SelectedItemChangeEvent to a ICommand in my view model. Everything seems to be working fine however I do not know how to get the SelectedItem of the ComboxBox. I think I need to use the CommandParameter of the EventToCommand - do I bind this to something in my ViewModel that has the selectedItem of the ComboBox? I've tried this:

<ComboBox 
  Width="422"
  Height="24"
  DisplayMemberPath="Name"
  ItemsSource="{Binding CategoryTypes}"
  SelectedItem="{Binding SelectedCategory}"
  >
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <MvvmLight:EventToCommand 
              Command="{Binding SelectCategoryCommand,Mode=TwoWay}"
              CommandParameter="{Binding SelectedCategory, Mode=TwoWay}"
              MustToggleIsEnabledValue="True" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ComboBox>

在我的视图模型中:

public ICommand SelectCategoryCommand
{
    get
    {
        return new SelectCategoryCommand(this);
    }
}

public CategoryType SelectedCategory
{
    get; set;
}

和ICommand

public class SelectCategoryCommand : ICommand
{
    private RowViewModel _rowViewModel;

    public SelectCategoryCommand(RowViewModel rowViewModel)
    {
        _rowViewModel = rowViewModel;
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        CategoryType categoryType = (CategoryType) parameter;
    }

}

但是,ICommand的Execute方法中的Parameter始终为null.我对SilverLight完全没有经验,所以我认为这里确实缺少明显的东西.有人可以帮忙吗?预先感谢!

However the Parameter in the Execute method of the ICommand is always null. I'm stil quite inexperienced with SilverLight so I think I'm really missing something obvious here. Can anyone help? Thanks in advance!

推荐答案

进行一些挖掘之后,我发现将实际的SelectionChangedEventArgs作为ICommand的execute参数非常简单:

After doing some digging I found that it is very simple to pass the actual SelectionChangedEventArgs as ICommand's execute parameter:

只需设置PassEventArgsToCommand="True"

<ComboBox Width="422"
          Height="24"
          DisplayMemberPath="Name"
          ItemsSource="{Binding CategoryTypes}"
          SelectedItem="{Binding SelectedCategory}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <MvvmLight:EventToCommand Command="{Binding SelectCategoryCommand,Mode=TwoWay}"
                                      MustToggleIsEnabledValue="True" 
                                      PassEventArgsToCommand="True"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ComboBox>

然后在Execute方法中执行以下操作:

And then in the Execute method do something like:

public void Execute(object parameter)
{
    SelectionChangedEventArgs e = (SelectionChangedEventArgs)parameter;
    CategoryType categoryType = (CategoryType)e.AddedItems[0];
}

这篇关于Mvvm-Light Silverlight,将EventToCommand与组合框配合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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