Forum Cross Platform with Xamarin

Button click event works only on first grid column

Hi all,

I know it sounds i little bit strange, but maybe i am doing something wrong on my code.

I am developing a ContentPage containing a grid, inside this grid i dynamically add a button with an event, like the code below:

`List<Model.Style> obj = new List<Model.Style>();
        Task.Run(async () =>
        {
            obj = await RefreshDataAsync();
        }).Wait();
        int intRow = 0;
        int intColumn = 1;

        foreach (var item in obj)
        {
            var navButton = new CustomButtonGradient
            {
                Text = item.Description,
                TextColor = Color.White,
                //FontFamily = Fonts.SFUiText,
                FontAttributes = FontAttributes.Bold,
                FontSize = 14,//Device.GetNamedSize(NamedSize.Small, typeof(CustomButtonGradient)),
                Image = "black_check_icon.png",
                //BackgroundStartColor = Color.FromHex("D96B57"),
                //BackgroundEndColor = Color.FromHex("D6523A"),
                BackgroundColor = Color.Red,
                BorderRadius = 11,
                HeightRequest = 32,
                WidthRequest = 86,
                VerticalOptions = LayoutOptions.Center,
                //Margin = new Thickness(1, 0, 1, 0),
                //CommandParameter = item
            };
            navButton.Clicked += MyButton_Clicked;
            if(intColumn%4==0)
            {
                intColumn = 1;
                intRow += 1;
            }
            gvMain.Children.Add(navButton,intColumn,intRow);
            intColumn += 1;
        }`

So, when i run this code on my iphone simulator, the button event works only on first column of each grid row. On the another columns, when button is clicked, nothing is raised.

Is there something wrong or is there anything i can do to workaround this issue?

Posts

  • juanzeta33juanzeta33 USMember ✭✭

    You want to make diferent action for each buttons or all buttons make the same acction?

    If it's different, I think that you need a differents names for each button and differents name for each eventHandler because I see your code and all the buttons adding make the same eventHandler method.

    navButton.Clicked += MyButton_Clicked;

  • RafaelAddessoRafaelAddesso BRMember ✭✭
    Hi @juanzeta33, i am adding the same event, and i use the eventarg to run some actions, but the event is run only on first column of each row.
  • juanzeta33juanzeta33 USMember ✭✭

    @RafaelAddesso said:
    Hi @juanzeta33, i am adding the same event, and i use the eventarg to run some actions, but the event is run only on first column of each row.

    I was reading, you can use the same eventHandler but not the same name of button. For change the name of the button dinamically, use the Name property of that button in the initialization!

  • RafaelAddessoRafaelAddesso BRMember ✭✭
    I dont think is possible to set the name property on initialization, i know that i can set the StyleId and AlternateId,but not the name
Sign In or Register to comment.