Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Accessing control with Dynamic name

I have various controls like label,entry in ListView where x:Name is assigned to these controls by dynamically generated uniqueid. I am not able to access these controls in code behind by this unique-id. So need help in these regards


  • JohnHardmanJohnHardman GBUniversity admin

    @BhaveshBanker.8046 - Do you mean code behind, or are you using Xamarin.UITest?

    If code behind, if you're generating the unique ids, you must know the logic as to how they were created. Why can't you use the same logic to access them?

    It might help if you post your XAML and code behind in order to explain the problem better.

  • Hi @JohnHardman and @N_Baua ,

    This is fragment of the Xaml


    .........Some Controls

    .........Some Controls

    Here for example these two stacklayout are in ListView on the tap of first stacklaout I need to hide second stacklayout that I tried to access through FindByName method and used name defined in x:Name but not able to access it.

  • N_BauaN_Baua INMember ✭✭✭✭✭

    Hi @BhaveshBanker.8046

    I guess you're mixing two concepts here:
    Event+Code-Behind v/s Delegate+Command

    I am assuming that your event is getting fired correctly.
    If that is the case and if you're working on Events+Code-behind x:Name should be very much accessible.
    and just ControlName.IsVisible = false or true should do the trick. No Binding is required (Rather will not work) and Vice versa if you are using MVVM following Delegate+Command routine, use the Binding property and an appropriate delegate-command should be easy to set the binding property, notifier will play the visibility for you.

    Below example (though not with ListView) done with binding properties, Switch and Grids.

    Hope it helps.

  • Hi @N_Baua ,

    I agree with your comment regarding IsVisible. But real problem is that if my x:Name is constant "xyz", it is accessible . But here my requirement is to set it through Binding x:Name={Binding UniqueId}". In this scenario it is not accessible.

  • N_BauaN_Baua INMember ✭✭✭✭✭
    edited August 2017


    Have you tried something like this to find the control

    Label myLabel = myStackLayout.FindByName

    This in code-behind, so that suggests, you could pass a name like "myLabel" + counter as well for the parameter above.
    What do you think?

  • Hi @N_Baua ,

    I have tried above thing and it does not work

  • N_BauaN_Baua INMember ✭✭✭✭✭

    Share your code

  • I have attached sample project in whihc ItemsPage.xaml.cs file I tried to access control by Name which give me null value

  • Hi @N_Baua ,

    Please see previous reply for sample code

Sign In or Register to comment.