Search code examples
listviewxamarin.forms

pass data in listview on button clicked


i want to pass the data of the itemselected to another page. The below code is working as expected.

private void inProgress_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            var surveyTapped = (SurveyList)e.SelectedItem;
            Navigation.PushAsync(new StartSurvey(surveyTapped.vchar_Title, surveyTapped.int_SurveyID, surveyTapped.int_UserSurveyID));            
        }

But i have 2 button in the listview and i have to add on clicked on those buttons

My UI

private void resume_ButtonClicked(object sender, EventArgs e)
        {
            //How and what should i implement in order to pass the data 
        }

Solution

  • Here is what i did that worked perfectly for me. i Used a commandparameter to pass the value. This is what i did in the listview

    <Button Text="Resume" TextColor="White" BackgroundColor="Green" HorizontalOptions="EndAndExpand" Clicked="resume_ButtonClicked" CommandParameter="{Binding .}"/>
    

    and onclicked event i gave the button a reference to the listview.

    private void resume_ButtonClicked(object sender, EventArgs e)
            {
                var menuItem = sender as Button;
                var selectedItem = menuItem.CommandParameter as SurveyList; 
                Navigation.PushAsync(new StartSurvey(selectedItem.vchar_Title, selectedItem.int_SurveyID, selectedItem.int_UserSurveyID));
            }