开发者

Mvvm-Light Silverlight, using EventToCommand with a Combobox

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>

and in my view model:

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

public CategoryType SelectedCategory
{
    get; set;
}

and the 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;
    }

}

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!


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

Just set 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>

And then in the Execute method do something like:

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


You may try adding a CommandParameter and passing a list to your relayCommand

Something similar is described towards the bottom of this page but with a datagrid: http://mvvmlight.codeplex.com/ The code from that page looks like this:

<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>

If you do this, your relayCommand would need to deal with the parameters coming in. Something like this in your 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;
    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜