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

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

问题描述

我已将 ComboBox 的 SelectedItemChangeEvent 连接到视图模型中的 ICommand.一切似乎都运行良好,但我不知道如何获取 ComboxBox 的 SelectedItem.我想我需要使用 EventToCommand 的 CommandParameter - 我是否将它绑定到我的 ViewModel 中具有 ComboBox 的 selectedItem 的东西?我试过这个:

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 的执行参数传递非常简单:

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天全站免登陆