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