Articles in this section
Category / Section

How to navigate a page while tapping .NET MAUI ListView's item using MVVM?

4 mins read

The .NET MAUI ListView (SfListView) allows you to navigate to another page when tapped on an element in the ListViewItem by adding the GestureRecognizer to that element in the ItemTemplate.

XAML

Add TapGestureRecognizer to the Image and bound the Command to navigate the page when tapped on that image from ViewModel.


<ContentPage.Content>
        <Grid>
            <listView:SfListView x:Name="list" ItemSize="70" ItemsSource="{Binding contactsinfo}" ItemSpacing="0,0,5,0" SelectionMode="Single">
                <listView:SfListView.ItemTemplate>
                    <DataTemplate x:Name="ItemTemplate">
                        <ViewCell>
                            <ViewCell.View>
                                <Grid x:Name="grid" RowSpacing="1">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="1" />
                                    </Grid.RowDefinitions>
                                    <Grid RowSpacing="1" Padding="10,0,0,0">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="50" />
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <Image Source="{Binding ContactImage}"
                           VerticalOptions="Center"
                           HorizontalOptions="Center"
                           HeightRequest="50"/>
                                        <Grid Grid.Column="1"
                          RowSpacing="1"
                          Padding="10,0,0,0"
                          VerticalOptions="Center">
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="*" />
                                                <RowDefinition Height="*" />
                                            </Grid.RowDefinitions>
                                            <Label LineBreakMode="NoWrap"
                             TextColor="#474747"
                             Text="{Binding ContactName}"
                                                   FontSize="12">
 
                                            </Label>
                                            <Label Grid.Row="1"
                             Grid.Column="0"
                             TextColor="#474747"
                             LineBreakMode="NoWrap"
                             Text="{Binding ContactNumber}"
                                                   FontSize="12">
 
                                            </Label>
                                        </Grid>
                                        <Grid Grid.Row="0"
                          x:Name="temp"
                          Grid.Column="2"
                          RowSpacing="0"
                          HorizontalOptions="End"
                          Padding="0,10,10,0">
                                            <Image Source="navigate.png">
                                                <Image.GestureRecognizers>
                                                    <TapGestureRecognizer
                              Command="{Binding Path=BindingContext.TapCommand, Source = {x:Reference list}}" CommandParameter="{Binding}" />
                                                </Image.GestureRecognizers>
                                            </Image>
                                        </Grid>
                                    </Grid>
                                    <StackLayout Grid.Row="1" BackgroundColor="#E4E4E4" HeightRequest="1"/>
                                </Grid>
                            </ViewCell.View>
                        </ViewCell>
                    </DataTemplate>
                </listView:SfListView.ItemTemplate>
            </listView:SfListView>
        </Grid>
    </ContentPage.Content>

C#

 In the ViewModel, navigate to another page through INavigation interface when the tap command is invoked.

public class ContactsViewModel : INotifyPropertyChanged
    {
        #region Fields
        private Command<object> tapCommand;
        private INavigation navigation;
        #endregion
 
        #region Properties
 
        public ObservableCollection<Contacts> contactsinfo { get; set; }
 
        public Command<object> TapCommand
        {
            get { return tapCommand; }
            set { tapCommand = value; }
        }
        public INavigation Navigation
        {
            get { return navigation; }
            set { navigation = value; }
        }
 
        #endregion
 
        #region Constructor
 
        public ContactsViewModel(INavigation _navigation)
        {
            navigation = _navigation;
            tapCommand = new Command<object>(OnTapped);
         }
 
               private void OnTapped(object obj)
        {
            var newPage = new Page1();
            newPage.BindingContext = obj;
            Navigation.PushAsync(newPage);
        }
}

View sample in GitHub

Navigate from one page to another by tapping the listview item

Take a moment to peruse the documentation to learn more about Tapping event in the SfListView with code examples.


Conclusion

I hope you enjoyed about how to navigate a page while tapping .NET MAUI ListView's item using MVVM (SfListView).

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. You can also explore our .NET MAUI Listview example 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 forums, Direct-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