Articles in this section

How to get the data model of the tapped item loaded in listview ?

You can get the underlying data model of the tapped item loaded in any list view template (SwipeTemplates and ItemTemplate) using commands from ViewModel.

<ContentPage xmlns:syncfusion="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms">
 
    <syncfusion:SfListView x:Name="listView" 
                           AutoFitMode="Height"                                    
                           ItemsSource="{Binding BookInfoCollection}">
        <syncfusion:SfListView.ItemTemplate>
            <DataTemplate>
                <Grid x:Name="grid">
                    <Grid.GestureRecognizers>
                        <TapGestureRecognizer Command="{Binding Path=BindingContext.ItemTappedCommand, Source={x:Reference listView}}" CommandParameter="{Binding .}}"/>
                    </Grid.GestureRecognizers>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="2*" />
                    </Grid.RowDefinitions>
                    <Label x:Name="bookName" Text="{Binding BookName}"/>
                    <Label Grid.Row="1" Text="{Binding BookDescription}"/>
                </Grid>
            </DataTemplate>
        </syncfusion:SfListView.ItemTemplate>
    </syncfusion:SfListView>
</ContentPage>

 

public class BookInfoRepository : INotifyPropertyChanged
{    
    private Command itemTappedCommand;
 
    public Command ItemTappedCommand
    {
        get { return itemTappedCommand; }
        protected set { itemTappedCommand= value; }
    }
 
    public BookInfoRepository()
    {
        itemTappedCommand = new Command(OnItemTapped);
    }
 
    ///<summary>
    ///To display tapped item content
    ///</summary>
    public void OnItemTapped(object obj)
    {
        var itemData = obj as BookInfo;
        App.Current.MainPage.DisplayAlert("Message", "Tapped item data: " + itemData.BookName, "OK");    }
}

 

Output:

Machine Learning Using C#

 

Click here to download the sample.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied