Is there a way to go to the bottom of ListView using the MVVM pattern? I do that using codebehind, but I'd like to do it using MVVM, if possible.
I use the ScrollTo() method of ListView.
Thanks
Is there a way to go to the bottom of ListView using the MVVM pattern? I do that using codebehind, but I'd like to do it using MVVM, if possible.
I use the ScrollTo() method of ListView.
Thanks
Copyright © 2021 Jogjafile Inc.
One way to do this is to attach a delegate method to your ViewModel and invoke the delegate. Another approach would be to use an event handler. You can also subscribe to the
CollectionChangedevent if you use anObservableCollection. With either approach you're not breaking the MVVM pattern, because you're applying inversion of control.Note that in MVVM the ViewModel must not know anything about Views, therefore it also cannot know that the
ScrollTo()method even exists. That said, you won't get around calling theScrollTo()method from within your View's code-behind.Solution using delegate
ViewModel
In your ViewModel, add and call a delegate like so:
View
Then, in your View's code behind, you can set the delegate:
In your View's XAML, you bind to the command:
You could just as well implement your own event and subscribe to that or use the built-in events of the
ObservableCollection. That's up to you, but the underlying concept would be the same. You cannot get around having the call toScrollTo()live somewhere in your View`s code behind without breaking MVVM.