我已经将ComboBox的SelectedItemChangeEvent与ViewModel中的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方法中的参数始终为null。我在SilverLight方面还很不熟练,因此我认为我确实错过了一些明显的东西。有谁能帮忙吗?先感谢您!