Init Groups collapse after ItemSource refresh

Hello, I am using SfListView in MVVM pattern. I use grouping by custom descriptor set in Behavior OnAttachedTo().


I need to init groups collpased / expanded according to data contained. This is working perfectly during inital setup in the "listView_Loaded" event handler. But I want to achive this, when I reload the data in ViewModel and call OnPropertyChanged("FormData").


I have tried to do it in following code, but it is handling somehow of the old data, not the new one.


<code>

private void listView_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)

{

   if (e.PropertyName == "ItemsSource") { /*... collapse here */}


}


</code>


1 Reply

LN Lakshmi Natarajan Syncfusion Team October 19, 2021 10:10 AM UTC

Hi Peter, 
 
Thank you for using Syncfusion products. 
 
We have checked the reported query “Init Groups collapse after ItemSource refresh” from our side. We would like to inform you that you can achieve your requirement by using the ListView.DataSource.AutoExpandGroups as False. It will allow you to collapse/expand the groups on initial load and when the ItemsSource is refreshed. 
 
protected override void OnAttachedTo(ContentPage bindable) 
{ 
    ListView = bindable.FindByName<SfListView>("listView"); 
    ListView.AllowGroupExpandCollapse = true; 
    ListView.DataSource.AutoExpandGroups = false; 
             
    ListView.DataSource.GroupDescriptors.Add(new Syncfusion.DataSource.GroupDescriptor() 
    { 
        PropertyName = "ContactName", 
        KeySelector = (object obj) => 
        { 
            var item = obj as Contacts; 
            return item.ContactName[0].ToString(); 
        } 
    }); 
    base.OnAttachedTo(bindable); 
} 
 
Please refer to our user guidance document regarding the same, 
 
You can also collapse particular item in the PropertyChanged event by getting the updated items details from the ListView.DataSource.DisplayItems property. Please refer to the following code snippets for more reference, 
 
private void ListView_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) 
{ 
    if (e.PropertyName == "ItemsSource") 
    { 
        var displayItems = ListView.DataSource.DisplayItems; 
 
        if (displayItems.Count > 0) 
        { 
            //Collapse particular group 
            Device.BeginInvokeOnMainThread(() => 
            { 
                var group = displayItems[0] as GroupResult; 
                group.Collapse(); 
            }); 
        } 
    } 
} 
 
Please refer to our user guidance document to expand/collapse a group programmatically from the following link, 
 
Please let us know if you need further assistance. 
 
Lakshmi Natarajan 
 


Loader.
Up arrow icon