Reputation: 878
I have ScrollView inside of a DataTemplate of a ListView. I have a need to horizontally auto scroll the scrollview for each item (to a different point determined by a value inside of my ViewModel which the ListItemViews are bound to).
To my knowledge there's no way to bind the scroll position. How can I call the ScrollView.ScrollToAsync method on the ScrollView inside the DataTemplate?
Thanks!
Upvotes: 1
Views: 401
Reputation: 15806
You can try to create a bindableProperty
of ScrollView
and bind your scroll value to this property, call the ScrollToAsync
method in propertyChanged
event:
public class CustomScrollView : ScrollView
{
public static readonly BindableProperty offSetProperty = BindableProperty.Create(
propertyName: nameof(offSet),
returnType: typeof(int),
declaringType: typeof(CustomScrollView),
defaultValue: 0,
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: ScrollOffsetChanged
);
static void ScrollOffsetChanged(BindableObject bindable, object oldValue, object newValue)
{
var view = (CustomScrollView)bindable;//here you should check if the bindable is CustomScrollView, I don't see your xaml
view.offSet = (int)newValue;
view.ScrollToAsync(view.offSet, 0, true);
}
public int offSet
{
get { return (int)GetValue(offSetProperty); }
set { SetValue(offSetProperty, value); }
}
}
Upvotes: 1