How to find the child controls from StackLayout dynamically ?

I am using Xamarin.Forms (PCL project). I have created the stacklayout with some child controls. How can I get any control from this stacklayout dynamically (like findbyName , findbyID or Type) ?

Tagged:

Answers

  • jerrymaguirejerrymaguire USMember

    Thanks AndreiNitescu,
    But I am not using Xaml and all are created by code behind . How can I set the property 'x : Name' in this case ?

  • AndrewMobileAndrewMobile USMember ✭✭✭✭
    edited June 2015

    you can't set the x:Name from code behind.
    the name is registered internally by Xamarin Forms's XAML parser

    since you're doing everything by code, you can just keep a reference to the instance of the control.

    I really suggest you switch to XAML. there are important performance improvements coming very very soon, if this the reason which you don't use XAML already

  • jerrymaguirejerrymaguire USMember

    I already designed more controls in Xaml :) . But for particular scenario I need to create the dynamic controls and access it dynamically. Now I tried to loop the child controls of parent controls to find it. I just having curious to know any other options available. Thanks anyway

  • AndrewMobileAndrewMobile USMember ✭✭✭✭

    It's still not clear to me why would you need to set a Name, but if you really need something like a tag, you can always create an attached property

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Use your own Dictionary.

  • ValdirRosendoValdirRosendo BRMember ✭✭

    I have the same problem:

    My form is generated dynamically because the form is created on server, after user fill out the form i need to get all informations filled on mobile and send to server, because of this necessity is need to set "Id" or "Name" in all objects in form.

  • RohitKumarRohitKumar USMember, University

    Is that you are getting a json with dynamic controls.. If yes, then you may ask for controltype in the same json and then leverage generics to assign properties and display them.

  • KorayemKorayem EGMember ✭✭

    Source: https://forums.xamarin.com/discussion/comment/168799/#Comment_168799

    x:Name just tells the compiler to make the backing field for you. So if you say var mybutton = new Button(). That is the same as x:Name="mybutton"

  • EhsanJahanagiriEhsanJahanagiri USMember ✭✭
    edited December 2017

    depends on your plan
    some ways
    1. foreach(View i in ((StackLayout)this.Content).Children.Where(x => x.GetType() == typeof(Label))) { }
    2. foreach (View i in ((StackLayout)this.Content).Children.Where(x => x.ClassId == "classId")) { }
    3. Label label = somelayoutchild.Parent.FindByName

  • CamelCaseMasterCamelCaseMaster USMember ✭✭
    var Label = new Label(){Text="Toto"};
    var id = Label.Id //(Guid Id)
    
    var text = (MyStackLayout.Children.Where(x => x.Id == id).FirstOrDefault() as Label).Text;
    
  • DeepakDYDeepakDY INMember ✭✭✭
    edited May 24

    Find children control by parent control

    I generating stacklayout tap and label is in stacklayout.

    this statement work for me :)

    private async void OrderDetailTapped(object sender, EventArgs e)
     {
          var button = sender as StackLayout;
          Label label =(Label)button.Children[0];
     }
    
Sign In or Register to comment.