Call listview.ScrollTo method from MVVM

Liuk88Liuk88 ITMember ✭✭

Hello guys, Im trying to realize a ScrollTo to a specific position of my listview.
Since Im using MVVM approach, in myxaml I haven't x:Name = myListview but I just have the ItemsSource = "{Binding myList}"
I noticed that the scrollTo is not a bindable property (you have to do listview.scrollTo in codebehind usually), so how can I scrollTo a specific element from MVVM if I don't have a reference to the listview Object directly?
Thanks!

Best Answers

Answers

  • NMackayNMackay GBInsider, University mod

    @GaetanF said:
    Create an ExtendedScrollView and expose the ScrollTo method into a read only bindable ICommand property. Bind it to your view model corresponding command and use it from there.

    I'd go with this approach.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    @NMackay said:

    @GaetanF said:
    Create an ExtendedScrollView and expose the ScrollTo method into a read only bindable ICommand property. Bind it to your view model corresponding command and use it from there.

    I'd go with this approach.

    I know you're a purist

  • Liuk88Liuk88 ITMember ✭✭

    Hello all, thanks for the possible solutions, before read these comments I solved using MessagingCenter (like 5 lines of code).
    The view model sends a message containing the list item I want to scroll to.
    The code behind subscribes to the message, when the message arrives it execute the scrollTo method.
    Seems really easy and short, what do you think about using this approach respect to the others you suggested?

  • NMackayNMackay GBInsider, University mod

    @Liuk88 said:
    Hello all, thanks for the possible solutions, before read these comments I solved using MessagingCenter (like 5 lines of code).
    The view model sends a message containing the list item I want to scroll to.
    The code behind subscribes to the message, when the message arrives it execute the scrollTo method.
    Seems really easy and short, what do you think about using this approach respect to the others you suggested?

    It's a hack really, message center is useful when you need to message between disconnected components etc, sometimes useful when sending notifications to multiple listeners you you have to be careful with message center, unregistered messages causes pages to hang about in memory. Things get out of control quickly with lots of messages flying about...in this case for a scenario that can be handle with a bindable property. Message center will work but it's suggest it's a "quick and dirty" fix. I'd knock it back if I got that branch to review :smile:

  • Liuk88Liuk88 ITMember ✭✭

    Hi NMackay, thanks for the clear explanation, was really useful :)
    Thanks to Gaetan and Alessandro too for the two different approaches.

  • LandLuLandLu Member, Xamarin Team Xamurai

    @Liuk88 If you thought other community members pointed out the correct direction. Try to mark that as an answer.

Sign In or Register to comment.