Accessing control with Dynamic name

BhaveshBanker.8046BhaveshBanker.8046 ✭✭USMember ✭✭

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

Answers

  • JohnHardmanJohnHardman mod GBUniversity mod

    @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.

  • BhaveshBanker.8046BhaveshBanker.8046 ✭✭ USMember ✭✭

    Hi @JohnHardman and @N_Baua ,

    This is fragment of the Xaml


    <StackLayout.GestureRecognizers>


    </StackLayout.GestureRecognizers>
    ..........
    .........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.

  • BhaveshBanker.8046BhaveshBanker.8046 ✭✭ USMember ✭✭

    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

    @BhaveshBanker.8046

    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?

  • BhaveshBanker.8046BhaveshBanker.8046 ✭✭ USMember ✭✭

    Hi @N_Baua ,

    I have tried above thing and it does not work

  • N_BauaN_Baua ✭✭✭✭✭ INMember ✭✭✭✭✭

    Share your code

  • BhaveshBanker.8046BhaveshBanker.8046 ✭✭ USMember ✭✭

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

  • BhaveshBanker.8046BhaveshBanker.8046 ✭✭ USMember ✭✭

    Hi @N_Baua ,

    Please see previous reply for sample code

Sign In or Register to comment.