如何将索引值绑定到ScrollView的ScrollToAsync属性中的元素参数-Xamarin表单 [英] How can I bind index value to element param in ScrollToAsync property of ScrollView - xamarin forms
问题描述
我是编程新手.我正在尝试将scrollview中的元素的索引值绑定到名为ScrollToAsync的scrollview属性,以便它会自动将选定的scrollview元素居中.
I am new to programming. I am trying to bing index value of an element in scrollview to a scrollview property called ScrollToAsync, so that it will automatically centre the selected element of scrollview.
下面是我的INotifyPropertyChanged代码
Below is my code for INotifyPropertyChanged
class LVItem : INotifyPropertyChanged
{
private int _tIndex;
public int TIndex
{
get { return _tIndex; }
internal set
{
_tIndex = value;
OnPropertyChanged("TIndex");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
和ScrollView的ScrollToAsnyc属性
and the ScrollToAsnyc property of ScrollView
scroll.ScrollToAsync(sLayout.Children[index], ScrollToPosition.Center, true);
我想在这里绑定索引". 我们可以绑定它吗?如果是,怎么办?请对此提供帮助.
I want here 'index' to be binded.. can we bind it? If yes, how? please help me on this..
下面是水平滚动视图的代码
Below is the code of horizontal scrollview
//horizontal list
StackLayout sLayout = new StackLayout()
{
Orientation = StackOrientation.Horizontal
};
for (int i = 0; i<itemLst.Count; i++)
{
Label label = new Label()
{
HorizontalTextAlignment = TextAlignment.Center,
TextColor = Color.Black,
FontSize = Device.GetNamedSize(NamedSize.Medium, new Label())
};
label.Text = itemLst[i];
gestureRecognizer = new TapGestureRecognizer
{
Command = new Command(TapL_Tapped),
CommandParameter = label,
};
label.GestureRecognizers.Add(gestureRecognizer);
sLayout.Children.Add(label);
}
ScrollView scroll = new ScrollView
{
Orientation = ScrollOrientation.Horizontal,
Content = new StackLayout
{
Children =
{
sLayout
}
}
};
推荐答案
您可以使用自定义可绑定Index属性创建自定义ScrollView
.
You can create a custom ScrollView
with a custom bindable Index property.
public class CustomScrollView : ScrollView
{
public static readonly BindableProperty IndexProperty = BindableProperty.Create (
propertyName: nameof (Index),
returnType: typeof (int),
declaringType: typeof (CustomScrollView),
defaultValue: 0,
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: HandleIndexChanged
);
static void HandleIndexChanged (BindableObject bindable, object oldValue, object newValue)
{
var view = (CustomScrollView)bindable;
view.Index = (int)newValue;
// Call your view.ScrollToAsync here.
// Depending on the structure of your XAML you could call
// it using view.Content to go through the control tree.
// Such as (view.Content as StackLayout).Children[newValue]
}
public int Index
{
get { return (int)GetValue (IndexProperty); }
set { SetValue (IndexProperty, value); }
}
}
然后,您可以通过在XAML页中将此部分添加到ContentPage
声明中来引用此自定义ScrollView
,而不是普通的自定义ScrollView
:
You can then reference this custom ScrollView
in your XAML page instead of the normal one by adding this part to your ContentPage
declaration:
xmlns:custom="clr-namespace:[YourNamespace];assembly=[YourAssembly]"
并引用如下控件:
<custom:CustomScrollView Index="{Binding TIndex}" />
这篇关于如何将索引值绑定到ScrollView的ScrollToAsync属性中的元素参数-Xamarin表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!