Articles in this section
Category / Section

How to automatically scroll to bring the selected item into the view in .NET MAUI ListView (SfListView) ?

3 mins read

The .NET MAUI ListView (SfListView) allows you to automatically bring the selected item into the view when it changes at runtime by calling the ScrollToRowIndex method.

C#

In the SfListView.PropertyChanged event, get the SelectedItem index from the DisplayItems, adjust the index based on 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);
                }
            });
        }
    }
}

View sample in GitHub

Bring selected item to view in .NET MAUI SfListView

Take a moment to peruse the documentation to learn more about programmatic scrolling in SfListView with code examples.

 

Conclusion

I hope you enjoyed learning about how to automatically scroll to bring the selected item into the view in .NET MAUI ListView (SfListView).

You can refer to our .NET MAUI ListView’s feature tour page to know about its other groundbreaking feature representations. You can also explore our .NET MAUI ListView documentation to understand how to present and manipulate data.

For current customers, you can check out our WinForms components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our .NET MAUI ListView and other .NET MAUI components.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied