InvokeCommandAction
约 116 字小于 1 分钟
PrismC#MVVMWPF
2025-06-10
通过 InvokeCommandAction 调用 ViewModel 中的命令
<ListBox Grid.Row="1" Margin="5" ItemsSource="{Binding Items}" SelectionMode="Single">
<i:Interaction.Triggers>
<!-- This event trigger will execute the action when the corresponding event is raised by the ListBox. -->
<i:EventTrigger EventName="SelectionChanged">
<!-- This action will invoke the selected command in the view model and pass the parameters of the event to it. -->
<prism:InvokeCommandAction Command="{Binding SelectedCommand}" TriggerParameterPath="AddedItems" />
</i:EventTrigger>
</i:Interaction.Triggers>public DelegateCommand<object[]> SelectedCommand { get; private set; } = new DelegateCommand<object[]>(OnItemSelected);
private void OnItemSelected(object[] selectedItems)
{
if (selectedItems != null && selectedItems.Count() > 0)
{
SelectedItemText = selectedItems.FirstOrDefault().ToString();
}
}