Mvvm-Light Silverlight,使用带有Combobox的EventToCommand

时间:2010-06-28 09:02:06

标签: silverlight selecteditem mvvm-light

我已经在我的视图模型中将ComboBox的SelectedItemChangeEvent连接到ICommand。一切似乎工作正常,但我不知道如何获得ComboxBox的SelectedItem。我想我需要使用EventToCommand的CommandParameter - 我将它绑定到我的ViewModel中具有ComboBox的selectedItem的东西吗?我试过这个:

<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很缺乏经验,所以我觉得我真的错过了一些明显的东西。有人可以帮忙吗?提前谢谢!

2 个答案:

答案 0 :(得分:20)

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

只需设置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方法中执行以下操作:

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

答案 1 :(得分:7)

您可以尝试添加CommandParameter并将列表传递给relayCommand

在此页面的底部描述了类似的东西,但是有一个datagrid:     http://mvvmlight.codeplex.com/该页面的代码如下所示:

<sdk:DataGrid x:Name="MyDataGrid" ItemsSource="{Binding Items}">
   <i:Interaction.Triggers> 
   <i:EventTrigger EventName="SelectionChanged">  
   <cmd:EventToCommand  Command="{Binding SelectionChangedCommand}"
                        CommandParameter="{Binding SelectedItems, ElementName=MyDataGrid}" />
   </i:EventTrigger>
   </i:Interaction.Triggers>
</sdk:DataGrid>

如果你这样做,你的relayCommand将需要处理进来的参数。 在ViewModel中有类似的东西:

public RelayCommand<IList> SelectionChangedCommand{    get;    private set;}

...

SelectionChangedCommand = new RelayCommand<IList>(
    items =>
    {
        if (items == null)
        {
            NumberOfItemsSelected = 0;
            return;
        }
        //Do something here with the records selected that were passed as parameters in the list
        //example:  NumberOfItemsSelected = items.Count;
    });