我有下面这个XAML,其中包含一个后台代码中的SelectionChanged事件处理程序和ViewModel的另一个Command属性。我已将SelectedIndex属性设置为0。现在运行项目时,会调用后台代码处理程序,但不执行命令。我的目标是,在视图加载时,对于SelectedIndex=0,应该第一次执行Command。
<ComboBox Name="listComboBox" SelectionChanged="listComboBox_SelectionChanged" SelectedIndex="0" SelectedValuePath="Content" Margin="5,0" Height="35" Width="150" VerticalAlignment="Center" HorizontalAlignment="Left">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding ListTypeComboSelectionChangedCmd}" CommandParameter="{Binding ElementName=listComboBox, Path=SelectedValue}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<ComboBoxItem Content="ItemOne" />
<ComboBoxItem Content="ItemTwo" />
<ComboBoxItem Content="ItemThree" />
</ComboBox>
更新
代码后端事件处理程序:
private void listComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { }
ICommand object:
public ICommand ListTypeComboSelectionChangedCmd
{
get { return new RelayCommand<string>(ListTypeComboSelectionChangedCmdExec); }
private set;
}
ICommand Handler:
private void ListTypeComboSelectionChangedCmdExec(string listType) { }