WPF中ComboBox和ListBox控件在MVVMLight框架下绑定事件的方法

WPF中ComboBox和ListBox控件在MVVMLight框架下绑定事件的方法

添加引用:
使用System.Windows.Interactivity.dll,添加该dll到项目引用(下载链接:https://download.csdn.net/download/qq_43024228/13674433)

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

或者在MVVMLight中,直接引用就可以:

xmlns:i ="http://schemas.microsoft.com/expression/2010/interactivity"

一、ComboBox

<i:Interaction.Triggers>
     <i:EventTrigger EventName="SelectionChanged">
            <i:InvokeCommandAction Command="{Binding FamilySelectionChangedCommand}"   CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},Path=DataContext}"/>
     </i:EventTrigger>
</i:Interaction.Triggers>

或:

<i:Interaction.Triggers>
       <i:EventTrigger EventName="DropDownClosed">
               <i:InvokeCommandAction Command="{Binding GetFunctionCommand}" CommandParameter="{Binding ElementName=ComboBoxName,Path=Text}"/>
       </i:EventTrigger>
</i:Interaction.Triggers>

二、ListBox

<i:Interaction.Triggers>
       <i:EventTrigger EventName="SelectionChanged">
              <i:InvokeCommandAction Command="{Binding FuncCommand}" CommandParameter="{Binding ElementName=ListBoxName, Path=SelectedItem}"/>
       </i:EventTrigger>
</i:Interaction.Triggers>

版权声明:本文为qq_43024228原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。