Joyce de Lanna
Joyce de Lanna

Reputation: 1543

know which button was clicked (Xamarin.Forms)

I have many images that works like a button and were created programatically and their data is from the webservice...Then, I don't know how many buttons I will have. Each Button represents one item of my webservice list...and, when the button is clicked, I need to know which item it represents. How can I do that?

I have something like this

enter image description here

And I need, when the user clicks on one of these images I receive a name of the object it represents...or a id...I dont know...

I am creating it with this code:

void eteste()
    {
        int column = 0;
        int row = 0;

        lstCategorias = lstCategorias.OrderBy(o => o.nome).ToList();

        foreach (var item in lstCategorias)
        {
            StackLayout stack = new StackLayout();
            stack.VerticalOptions = LayoutOptions.Center;

            Image textura = new Image();
            textura.Source = "pngteste";
            textura.AutomationId = item.idCategoria;
            textura.HorizontalOptions = LayoutOptions.Fill;
            textura.VerticalOptions = LayoutOptions.Fill;
            gridteste.Children.Add(new BoxView { Color = Color.FromHex(item.corFundo) }, row, column);

            gridteste.Children.Add(textura, row, column);

            var CliqueCategoria = new TapGestureRecognizer();
            CliqueCategoria.Tapped += (s, e) => {


            };

            textura.GestureRecognizers.Add(CliqueCategoria);


            gridteste.Children.Add(stack, row, column);

            stack.Children.Add(new Image { Source = item.imagem, VerticalOptions = LayoutOptions.StartAndExpand, HorizontalOptions = LayoutOptions.Fill });

            stack.Children.Add(new Label { Text = item.nome, TextColor = Color.FromHex(item.corTexto), BackgroundColor = Color.Accent, VerticalOptions = LayoutOptions.CenterAndExpand, HorizontalOptions = LayoutOptions.CenterAndExpand });

            if (row == 0)
            {
                row = 1;
            }
            else
            {
                row = 0;
                column++;
            }

        }
    } 

I tried to use textura.AutomationId = item.id in the foreach in order to get the id after...and I used the CliqueCategoria.Tapped += (s, e) => with a **var x = s.AutomationId **, trying get the id of the item that the button represents, but it didn't work as well...

Upvotes: 1

Views: 1019

Answers (2)

Yehor Hromadskyi
Yehor Hromadskyi

Reputation: 3388

I would probably use BindingContext property for such purposes:

    foreach (var item in lstCategorias)
    {
        ...

        Image textura = new Image();
        textura.BindingContext = item;            

        ...

        var CliqueCategoria = new TapGestureRecognizer();
        CliqueCategoria.Tapped += (s, e) => {
            var image = s as Image;
            var itemFrom_lstCategorias = image.BindingContext as TypeOfTheItemFromForeach;
        };

        ...
    } 

Upvotes: 1

Yuri S
Yuri S

Reputation: 5370

You need to add TapGestureRecognizer to each image and add this image to (probably) Grid. When image is tapped you will receive "Image" object in "sender" parameter. You can extend an Image providing new get/set extension method for Id or derive from Image and add Id. You also can use Source member of Image which you set to "Source = item.imagem"

Upvotes: 2

Related Questions