Articles in this section
Category / Section

How to edit the ListView tapped item in Xamarin.Forms (SfListView) using DataForm?

1 min read

You can edit the tapped ListViewItem by binding the item to DataForm in Xamarin.Forms ListView.

XAML

ListView ItemTemplate defined with Edit button to edit the data.

<ContentPage xmlns:listView="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms">
    <ContentPage.Content>
        <Grid>
           <listView:SfListView x:Name="listView" 
                           ItemsSource="{Binding contactsinfo}" >
                        <listView:SfListView.ItemTemplate>
                            <DataTemplate>
                                <StackLayout >
                                <Grid>
                                            <Image Source="{Binding ContactImage}" />
                                            <StackLayout Grid.Column="1">
                                                <Label Text="{Binding ContactName}"/>
                                                <Label Text="{Binding ContactNumber}"/>
                                            </StackLayout>
                                            <Grid Grid.Column="2">
                                                <Button x:Name="ZoomIn" Text="Edit" Command="{Binding Path=BindingContext.TapCommand, Source={x:Reference Name=listView}}" 
                                            CommandParameter="{Binding .}" />
                                            </Grid>
                                        </Grid>   
                                    </StackLayout>                                    
                            </DataTemplate>
                        </listView:SfListView.ItemTemplate>
           </listView:SfListView>
        </Grid>
    </ContentPage.Content>
</ContentPage>

XAML

DataForm defined with TappedItem binding from ViewModel.

<Grid>
    <dataForm:SfDataForm x:Name="dataForm" 
                         LabelPosition="Top" CommitMode="PropertyChanged"
                         DataObject="{Binding TappedItem}"/>
    <Button Text="Done" Command="{Binding DoneCommand}" Grid.Row="1"/>
</Grid>

C#

On edit, Update the edited item to TappedItem property and navigate to DataForm page

public class ContactsViewModel : INotifyPropertyChanged
{
    private Contacts _tappedItem;
    public Command<object> TapCommand { get; set; }
    public Command<object> DoneCommand { get; set; }
 
    public Contacts TappedItem
    {
        get => _tappedItem;
        set {
            _tappedItem = value;
            OnPropertyChanged("TappedItem");
        }
    }
 
    public ContactsViewModel()
    {
        TapCommand = new Command<object>(ButtonTapped);
        DoneCommand = new Command<object>(DoneTapped);
    }
 
    private void DoneTapped(object obj)
    {
        App.Current.MainPage.Navigation.PopAsync();
    }
 
    private void ButtonTapped(object obj)
    {
        TappedItem = obj as Contacts;
        var dataForm = new EditPage();
        dataForm.BindingContext = this;
        App.Current.MainPage.Navigation.PushAsync(dataForm);
    }
}

Output

ListView Items                   DataFormPage

View sample in GitHub

 

Conclusion

I hope you enjoyed learning about how to edit the ListView tapped item in Xamarin.Forms (SfListView) using DataForm.

You can refer to our Xamarin.Forms ListView feature tour page to know about its other groundbreaking feature representations. You can also explore our Xamarin.Forms ListView documentation to understand how to create and manipulate data.

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 forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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