2

First of all I am new at IT developing, and I just learning... I have a RunTime button and I want to handle the event for each button, how can I do it? This is the code that I have;

  public partial class Dashboard : ContentPage
{

    private IList<Condominium> output;


    public Dashboard(IList<Condominium> output)
    {

        var Buttonadd = new Style(typeof(Button))
        {
            Setters = {
                        new Setter {Property = Button.TextColorProperty, Value = Constants.MainTextColor},
                        new Setter {Property = Button.BackgroundColorProperty, Value = Constants.BackgroundColor},
                        new Setter {Property = Button.HorizontalOptionsProperty, Value = LayoutOptions.FillAndExpand},
                        new Setter {Property = Button.VerticalOptionsProperty, Value = LayoutOptions.FillAndExpand},
                        new Setter {Property = Button.TextProperty, Value = TextAlignment.End},
                        new Setter {Property = Button.WidthRequestProperty, Value = 200},
                        new Setter {Property = Button.HeightRequestProperty, Value = 200},
                       }
        };



        StackLayout parent = new StackLayout();

        foreach (var cond in output)
        {
            Button add = new Button
            {
                Style = Buttonadd,
                Text = cond.Name,
                Margin = new Thickness(0, -10, 0, -10),

            };



            parent.Children.Add(add);


        }


        Content = new ScrollView
        {
            Margin = new Thickness(0, 10, 0, 10),
            BackgroundColor = Color.White,
            Content = parent,
        };


        this.output = output;


        InitializeComponent();
    }



}
}
1
  • Try add.Click += YourClickHandler; Commented Jun 30, 2017 at 10:36

1 Answer 1

1

You have to subscribe to the click event.

You can read up on the button event here

Also see EventHandler Delegate for a better understanding how the Clicked event handler works.

Button btnToAdd = new Button
{
    Style = Buttonadd,
    Text = cond.Name,
    Margin = new Thickness(0, -10, 0, -10),

};

btnToAdd.Clicked += OnButtonClicked;

parent.Children.Add(add);

and then add your event handler:

void OnButtonClicked(object sender, EventArgs e)
{
    //add your code here
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.