In this post, I am going to show you how we can easily extend CollectionView to implement ScrollTo-functionality through DataBinding from a ViewModel (for both grouped and non-grouped data sources).

If you are working with collections in your app, chances are high you are going to want (or need) to scroll to a specific item at some point. CollectionView has the ScrollTo method that allows you to do so. If you are using MVVM in your app however, there is no built-in support to call this method.

My solution

My solution for this challenge consists of following parts:

  • BindableProperty in an extended CollectionView class to bind the item we want to scroll to
  • a configuration class to control the scrolling behavior
  • a base interface with the configuration and two variants derived from it (one for ungrouped items, one for grouped ones)

Let’s have a look at the ScrollConfiguration class:

public class ScrollToConfiguration { public bool Animated { get ; set ; } = true ; public ScrollToPosition ScrollToPosition { get ; set ; } = ScrollToPosition. Center ; }

These two properties are used to tell our extended CollectionView how the scrolling to the item will behave. The above default values are my preferred ones, feel free to change them in your implementation.

#xamarin #binding #collectionview #github

Scroll to any Item in Your Xamarin.forms CollectionView From Your ViewModel
2.95 GEEK