Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

244
Views
Get control name in Button event handler method Xamarin Forms

I have 20 buttons in my Xamarin Forms app . All of the buttons share the same click event method. What I want to do is use switch statement to check the button name but I am having difficulty finding the controls name that fired the event .

Please see the code :

private  void btnCollection_Clicked(object sender, EventArgs args)
    {
        var btn = (Button)sender;

        switch (btn.FindByName) // I want to get the name of the control 
        {

            case "btn1":
                break;

                case "btn2":
                break;
        }
    }

How can I get the button's name?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You cannot access the x:Name property of a xaml element as this is just a hint for the compiler to name the variable.

What you can do however is to set the ClassId of your button so that you can retrieve it in the handler. Like this: Your xaml :

<Button ClassId="sdsd"
              Clicked="Button_OnClicked"/>

Your xaml.cs

private void LoginButton_OnClicked(object sender, EventArgs e)
        {
            var button = (Button) sender;
            var classId = button.ClassId;
        }
about 4 years ago · Santiago Trujillo Report

0

Maybe this can help you

if(sender is Button){

   Button button = (Button)sender;
   if(button.Equals(myButton1)){
      // You are in myButton1
   }else if(button.Equals(myButton2)){
   }
}
about 4 years ago · Santiago Trujillo Report

0

var btn = (Button)sender;

if(btn.Id == btn1.Id) {

}
else if(btn.Id == btn2.Id){
}
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!