【发布时间】:2020-06-05 02:57:40
【问题描述】:
我在 ListView 的 DataTemplate 中有 ScrollView。我需要水平自动滚动每个项目的滚动视图(到由 ListItemViews 绑定到的 ViewModel 中的值确定的不同点)。
据我所知,没有办法绑定滚动位置。如何在 DataTemplate 内的 ScrollView 上调用 ScrollView.ScrollToAsync 方法?
谢谢!
【问题讨论】:
我在 ListView 的 DataTemplate 中有 ScrollView。我需要水平自动滚动每个项目的滚动视图(到由 ListItemViews 绑定到的 ViewModel 中的值确定的不同点)。
据我所知,没有办法绑定滚动位置。如何在 DataTemplate 内的 ScrollView 上调用 ScrollView.ScrollToAsync 方法?
谢谢!
【问题讨论】:
您可以尝试创建ScrollView 的bindableProperty 并将滚动值绑定到此属性,在propertyChanged 事件中调用ScrollToAsync 方法:
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); }
}
}
【讨论】: