A for android
A for android

Reputation: 168

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 
        }

Upvotes: 2

Views: 2551

Answers (5)

Indian
Indian

Reputation: 31

First I created a button in ListView of xaml file

<Button Text="Submit" Clicked="btn_clicked" CommandParamter={Binding .}/>

Then we need to pass the data onclick of button

var data = (ModelName)((Button)sender).CommandParameter;

await Navigation.PushAsync(new AnotherPageName(data));

Upvotes: 1

Shepherdswork
Shepherdswork

Reputation: 11

I was a ble to catch a passed value through a button like this.

<Button  Clicked="delBtn_Clicked" CommandParameter="{Binding customerId}" x:Name="delBtn" Text="Delivered" HorizontalOptions="EndAndExpand" BackgroundColor="Black" TextColor="White"></Button>

private void delBtn_Clicked(object sender, EventArgs e)
{ 
  var ListItem = sender as Button;
  var currentItem = ListItem.CommandParameter.ToString();
  DisplayAlert("Alert", currentItem, "Ok");
}

Upvotes: 1

A for android
A for android

Reputation: 168

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));
        }

Upvotes: 3

iamdkrz
iamdkrz

Reputation: 21

Binding is a saviour here.

If you are going with Event oriented approach, try following code:

    private void resume_ButtonClicked(object sender, EventArgs e)
    {

        var selectedItem = (SurveyList)LISTVIEW.SelectedItem; // where LISTVIEW is the name of the listview.
        //Other actions
    }

Upvotes: 2

Saket Kumar
Saket Kumar

Reputation: 1197

Try this suggestion.

While adding Buttons to listview rows add them with unique "id" property, this way you would know which button is clicked and it lies on which row.

It would be like -> row 0 - Buttons id could be with 01,02 row 1- Buttons could be with 10,11 so on..

So when you click the button you would know which button is clicked and where does it lie.

When you know the listview row index, you can get the data from the itemsource property of the list at that index or use the collection you used to bind the data.

Now you have the data and you can pass the same you have done in first case.

here is the property I am talking about.

https://learn.microsoft.com/en-us/dotnet/api/xamarin.forms.element.id?view=xamarin-forms#Xamarin_Forms_Element_Id

Upvotes: 0

Related Questions