Advertisement
Intermediate .NET MAUI, Controls & Views

How do you handle item selection in a CollectionView?

Short answer

Set SelectionMode to Single or Multiple, then bind SelectedItem (or SelectedItems) and handle SelectionChanged, or bind SelectionChangedCommand. In MVVM you typically bind SelectedItem to a view-model property and react in its setter or via a command, avoiding code-behind.

Save .NET MAUI mock interview
Advertisement
AModel answer
Set SelectionMode to Single or Multiple, then bind SelectedItem (or SelectedItems) and handle SelectionChanged, or bind SelectionChangedCommand. In MVVM you typically bind SelectedItem to a view-model property and react in its setter or via a command, avoiding code-behind.
<CollectionView ItemsSource="{Binding Items}"
                SelectionMode="Single"
                SelectedItem="{Binding SelectedItem}"
                SelectionChangedCommand="{Binding OpenCommand}" />
Real-world example
Tapping a row in a contacts list sets SelectedItem, which the view model uses to navigate to a detail page via its command.
Likely follow-ups
  • What selection modes exist? | How do you clear selection?
Related topics