How can I access the elements on one view in the code of another view?

KING_SIDKING_SID Member

I am trying to go about binding and I want to bind a date picker that is on my navigation bar of a Parent Tabbed page as the SOURCE, and bind it to a Label's text which is the TARGET on one of the child Tab Pages (which is a Content Page), so that whenever I select a date in the navigation bar's datepicker, it updates the label on that child page to that date. Can I bind them directly?

Currently I the elements are have their binding set to a shared Date Object that is initialised when the app begins so this acts as a middle man. The issue is that it doesnt update the label's text when I change the date on the date picker unless I go to another page and then go back to that page.

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Stop.
    As mentioned in the other 100 threads of "How can I reach UI element from another page" type questions...
    you shouldn't be trying to reach UI elements at all. That shows a clear break from designing along a proper MVVM pattern.

    bind a date picker that is on my navigation bar of a Parent Tabbed page as the SOURCE

    Well... That's wrong. A DatePicker is not a source. The DatePicker should be binded to a property on your ViewModel.
    So your Label that you want to display text from the same source, should bind to the same property on the same VIewModel.

    the elements are have their binding set to a shared Date Object that is initialised when the app begins so this acts as a middle man

    That sounds correct. Its not a 'middle man'. Its the right way: One property used by a couple of UI elements.

    The issue is that it doesnt update the label's text when I change the date on the date picker

    Then your binding is not right. How or in what way... No telling. There's a thousand ways someone can make a mistake.

    My suggestion: Work this tutorial series end to end. 2 hours of your day is no big deal. Then take that understanding and apply it to your solution.
    http://redpillxamarin.com/2018/03/12/2018-101-vs2017-new-solution/

  • PenutButterJellyHeadPenutButterJellyHead USMember ✭✭✭

    No bigi! You can just raise the view property flag after you update the picker. This will then tell the view to update from the code behind on the view.

  • HotdogHeadHotdogHead Member ✭✭

    Could you show us a screen shot? And maybe the source code so we can see what it's doing? I bet it's not using an ObservableCollection but no way to be sure without seeing the code.

  • KING_SIDKING_SID Member

    @PenutButterJellyHead said:
    No bigi! You can just raise the view property flag after you update the picker. This will then tell the view to update from the code behind on the view.

    Hey @PenutButterJellyHead , How do I go about implementing this?

  • jezhjezh Member, Xamarin Team Xamurai
Sign In or Register to comment.