【问题标题】:Mvvm-Light Silverlight, using EventToCommand with a ComboboxMvvm-Light Silverlight,使用带有 Combobox 的 EventToCommand
【发布时间】:2011-03-09 01:13:52
【问题描述】:

我已将 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 非常缺乏经验,所以我认为我在这里确实遗漏了一些明显的东西。任何人都可以帮忙吗?提前致谢!

【问题讨论】:

    标签: silverlight selecteditem mvvm-light


    【解决方案1】:

    经过一番挖掘,我发现将实际的 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];
    }
    

    【讨论】:

    • 你应该标记你的答案,特别是因为它解决了你的问题。
    【解决方案2】:

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

    本页底部描述了类似的内容,但使用了数据网格: 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;
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      • 1970-01-01
      • 1970-01-01
      • 2017-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多