在MVVM中实现ComboBox的SelectionChanged
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
前台view页面的ComboBox :
<ComboBox Height="23" Margin="121.465,21.435,160.048,0"
VerticalAlignment="Top"
ItemsSource="{Binding SupplierList}"
DisplayMemberPath="Name"
SelectedItem="{Binding SelectedSupplier}">
//主要绑定事件
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding selectionChangedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
ViewModel中:
public Window1ViewModel()
{
//初始化selectionChangedCommand 事件
selectionChangedCommand = new DelegateCommand(SelectionChangedCommand, CanselectionChangedCommand);
}
ViewModel中要绑定的selectionChangedCommand事件:
//定义命令属性
public ICommand selectionChangedCommand { get; set; }
//命令的方法
public void SelectionChangedCommand(object param)
{
MessageBox.Show("OK");
}
//启用命令
public bool CanselectionChangedCommand(object param)
{
return true;
}
主要是对MVVM中ComboBox绑定SelectionChanged的演示,对于和我一样初学者分享!!