Home > Enterprise >  Xamarin forms retrieve student details Id on button click event handler
Xamarin forms retrieve student details Id on button click event handler

Time:10-03

I am wanting to create a set of dynamic buttons based on a list of students. In winforms days I would have had the tag property to store the ID of the student for easy retrieval

But in Xamrain Forms I dont have that how would I retrieve the student ID on my button click event handler to be able to retrieve the correct student Id details.

Also how does one gain access to the current button the student clicked on in winforms you would have accessed the array of buttons ?

public async void CheckInButtons()
{
    List<Booking>  myList = new List<Booking>(); //your list here
    var test = DayNumber;
    myList =await  api.GetAllBookingsForDay(DayNumber,Helpers.Dates.GetDateZeroTime(DateTime.Now).AddDays(1));


    //  checkinButtons.Children.Clear(); //just in case so you can call this code several times np..
    foreach (var item in myList)
    {
        string firstname = "Dan Check IN";
        string lastname = "Button";
        var btn = new Button()
        {
                
            BackgroundColor = Color.Blue,
            TextColor = Color.White,
            Text = firstname   " "   lastname,
               
            };
        btn.Clicked  = Btn_Clicked; 
        checkinButtons.Children.Add(btn);
    }

}

Here is my button click

private void Btn_Clicked(object sender, EventArgs e)
{
        var item = e.ToString();
        /// I need to get the student details here? How
        /// I also want to be able to disable the current button of the student 
        /// after they clicked it in winforms u could have goto the index number of the button how in xamrain?

        btnGotoExercise.IsVisible = true;

}

Thanks in advance

CodePudding user response:

For buttons I don't know how to help you.

BUT, if you don't mind, you could involve each button in a transparent Frame. Frame have a property called AutomationId. You can get it OR you can create a list of IDs and set each automationID with the IDs, maybe in a for.

The idea of this IDs could've been done in your original way (just Buttons), but you will need to associate this buttons with some variable, e.g: Create a field in your Booking model called ID and make it auto increment OR associate this field with the list of ID.

EDIT: As JamesMontemagno said and I tested it now, Buttons also has AutomationID (I remembered only for Frames because I use it just on Frames). So, go ahead with this idea of using AutomationID, but now don't need to use Frame to do it.

CodePudding user response:

I like the idea of using AutomationId, else you could set the BindingContext of the Button:

    foreach (var item in myList)
    {
        string firstname = "Dan Check IN";
        string lastname = "Button";
        var btn = new Button()
        {
                
            BackgroundColor = Color.Blue,
            TextColor = Color.White,
            Text = firstname   " "   lastname,
               
            };
        btn.Clicked  = Btn_Clicked; 
        btn.BindingContext = item; // add this here
        checkinButtons.Children.Add(btn);
    }

Then on click

private void Btn_Clicked(object sender, EventArgs e)
{
        var item = ((Button)sender).BindingContext as Booking;

        btnGotoExercise.IsVisible = true;

}

I would probably use a ListView though for something like this as the cell has a reference to the "Booking" that it is rendering.

  • Related