How to get the selected items using TapCommand in .NET MAUI ListView (SfListView)?
The .NET MAUI SfListView control allows multiple item selection using the SelectionMode property. To access the selected items within the TapCommand, bind the SelectedItems property to your ViewModel, which holds the collection of currently selected items, and process them inside your command.
XAML
<syncfusion:SfListView x:Name="listView"
ItemsSource="{Binding BookInfo}"
SelectionMode="Multiple"
SelectionGesture="Tap"
SelectedItems="{Binding SelectedItems}"
TapCommand="{Binding TapCommand}"
ItemSize="100">
<syncfusion:SfListView.ItemTemplate>
<DataTemplate>
<Grid Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="0.4*" />
<RowDefinition Height="0.6*" />
</Grid.RowDefinitions>
<Label Text="{Binding BookName}" FontAttributes="Bold" TextColor="Teal" FontSize="21" />
<Label Grid.Row="1" Text="{Binding BookDescription}" TextColor="Teal" FontSize="15"/>
</Grid>
</DataTemplate>
</syncfusion:SfListView.ItemTemplate>
</syncfusion:SfListView>
ViewModel
public class BookInfoRepository
{
private ObservableCollection<object>? selectedItems = new ObservableCollection<object>();
public ObservableCollection<object>? SelectedItems
{
get { return selectedItems; }
set { this.selectedItems = value; }
}
public ICommand TapCommand { get; set; }
public BookInfoRepository()
{
TapCommand = new Command(ExecuteTapCommamd);
}
private void ExecuteTapCommamd(object obj)
{
// SelectedItems contains the currently selected items.
var _selectedItems = this.SelectedItems;
}
}
Conclusion:
I hope you enjoyed learning about how to get selected items in viewmodel from TapCommand in .NET MAUI ListView.
You can refer to our .NET MAUI ListView feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!