How to automatically scroll to bring the selected item into the view in .NET MAUI ListView (SfListView) ?
The .NET MAUI ListView (SfListView) allows you to automatically bring the selected item into the view when it changes at runtime by using the ScrollToRowIndex method.
C#
In the SfListView.PropertyChanged event, obtain the SelectedItem index from the DisplayItems, adjust the index based on the header and group header, and pass it to the ScrollToRowIndex method.
public class ListViewBehavior : Behavior<SfListView> { private SfListView ListView; protected override void OnAttachedTo(SfListView bindable) { ListView = bindable; ... ListView.PropertyChanged += ListView_PropertyChanged; base.OnAttachedTo(bindable); } private void ListView_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { if (e.PropertyName == "SelectedItem") { ListView.Dispatcher.Dispatch(async () => { await Task.Delay(100); if (this.ListView.DataSource.DisplayItems.Count > 0) { var selectedItemIndex = ListView.DataSource.DisplayItems.IndexOf(ListView.SelectedItem); selectedItemIndex += (ListView.HeaderTemplate != null && !ListView.IsStickyHeader && !ListView.IsStickyGroupHeader) ? 1 : 0; (ListView.ItemsLayout as LinearLayout).ScrollToRowIndex(selectedItemIndex); } }); } } }
Download the complete sample on GitHub
Conclusion
I hope you enjoyed learning how to automatically scroll to bring the selected item into view in .NET MAUI ListView (SfListView).
You can refer to our .NET MAUI ListView feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. Explore our .NET MAUI ListView example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our other controls.
Please let us know in the comments section below if you have any queries or require clarification. Contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!