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.
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));
}
}
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
}
}
};
You can create a custom
ScrollView
with a custom bindable Index property.You can then reference this custom
ScrollView
in your XAML page instead of the normal one by adding this part to yourContentPage
declaration:And referencing the control as follows: