Home > OS >  is there any wany to have a double click event on listview and send list data to another page in Xam
is there any wany to have a double click event on listview and send list data to another page in Xam

Time:09-30

This is what I have done now I am unable to send ID to another page Getting error at Command Parameter as null reference exception

 <ListView x:Name="studentListView" ItemsSource="{Binding .} "> 
                    <ListView.ItemTemplate >
                        
                        <DataTemplate  >
                            <ViewCell>
                                <StackLayout Orientation="Horizontal">
                                    <Label Text="{Binding StudentID}"></Label>
                                    <Label Text="{Binding FirstName}"></Label>
                                    <Label Text="{Binding LastName}"></Label>
                                    <Label Text="{Binding Gender}"></Label>
                                    <Label Text="{Binding Age}"></Label>
                                    <Label Text="{Binding Class}"></Label>
                                    <StackLayout.GestureRecognizers>
                                        <TapGestureRecognizer NumberOfTapsRequired="1" Tapped="TapGestureRecognizer_Tapped" CommandParameter="{Binding StudentID}"/>
                                        <TapGestureRecognizer NumberOfTapsRequired="2" Tapped="TapGestureRecognizer_Tapped_1"/>
                                    </StackLayout.GestureRecognizers>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

C# Code

private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
        {
       TapGestureRecognizer tapGesture = sender as TapGestureRecognizer;
            int clubid = (int) tapGesture.CommandParameter;
            Navigation.PushAsync(new StudentPage(clubid));
        }

CodePudding user response:

You could also directly add a TapGestureRecognizer on the StudentID of the Label and then get the ID value to send it to another Page. Here's the code below for your reference:

Student Page Xaml:

    <ListView x:Name="studentListView" > 
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Orientation="Horizontal">
                                <Label Text="{Binding StudentID}" WidthRequest="100" >
                                    <Label.GestureRecognizers  >
                                        <TapGestureRecognizer NumberOfTapsRequired="2" Tapped="TapGestureRecognizer_Tapped">
                                        </TapGestureRecognizer>
                                    </Label.GestureRecognizers>
                                </Label>
                                <Label Text="{Binding FirstName}"/>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

Code in Back-end:

     public StudentList() 
        {

            InitializeComponent();

            studentListView.ItemsSource = new List<MyStudent>
            {

                new MyStudent{StudentID="1", FirstName="Steve"},
                new MyStudent{StudentID="2", FirstName="Lorence"}
            };
        }

        private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
        {
           //get the StudentID and pass it through constructor
            var s = (sender as Label).Text;
            Navigation.PushModalAsync(new MainPage(s));
        }

Then, get the StudentID via Constructor in another page.

  public MainPage(string a)
  {
       InitializeComponent();

       App.Current.MainPage.DisplayAlert("Student Id is", a, "OK");
  }

  • Related