我正在构建一个MVVM模式的程序我可以用非mvvm方式处理xaml.cs文件中的方法来处理selectionchanged事件。但不知道如何在MVVM中做到这一点。
目前我所有的iccommand属性都在viewmodel.cs文件中,查看代码如下
<StackPanel Grid.Row="0" Orientation="Horizontal" Margin="10,0" HorizontalAlignment="Right">
<Label Content="Ports" Margin="2" />
<ComboBox IsReadOnly="True" AllowDrop="True" x:Name="ComboBox_SerialPort" MinWidth="60" Margin="5"
ItemsSource="{Binding SerialPortList}" SelectedValue="{Binding SelectedSerialPort}"
SelectionChanged="{Binding Cmd_ComboBox_SerialDropDownSelectionChanged}" >
</ComboBox>
</StackPanel>
显然直接绑定ICommand属性到SelectionChanged是不工作的。但是在组合框控件中没有Command属性。那么我如何将iccommand附加到控制事件
感谢您可以在SelectedSerialPort
源属性的setter中处理您的选择更改逻辑,该属性将在您选择ComboBox
中的值时设置。
选项1:
<ComboBox>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding YourCommandName}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Combobox>
选项2:
<ComboBox SelectedValue="{Binding SelectedSerialPort, UpdateSourceTrigger=PropertyChanged}"/>