We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: Xamarin.Forms |
Control: SfListView |
Published Date: March 25, 2020 |
Last Revised Date: March 26, 2020

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

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

 

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile