1、原因
在WPF中单纯的命令绑定往往不能满足覆盖所有的事件,例如ComboBox的SelectionChanged事件,DataGrid的SelectionChanged事件等等,这时就可以用事件绑定来完成。
2、示例
事件绑定需要用到:System.Windows. interactivity.dll 库,如果安装了Blend for visual studio,里面就包含了这个dll,在Interaction.Triggers里面添加一个或多个EventTrigger并指定关注的的事件名称,在EventTrigger中通过InvokeCommandAction来绑定事件对应的命令。
代码如下:
<ComboBox Grid.Column="3" Width="90" SelectedItem="{Binding SelectConfirmStatus, Mode=TwoWay}" ItemsSource="{Binding ConfirmStatus, Mode=OneWay}" SelectedValuePath="ID" DisplayMemberPath="StatusName" Height="40" Foreground="Black" HorizontalContentAlignment="Left" VerticalContentAlignment="Center" HorizontalAlignment="Right" VerticalAlignment="Center" > <i:Interaction.Triggers> <i:EventTrigger EventName="SelectionChanged"> <i:InvokeCommandAction Command="{Binding ConfirmStatusCommand}"> </i:InvokeCommandAction> </i:EventTrigger> </i:Interaction.Triggers> </ComboBox>