Delete a group from a collectionview from observableCollection

Stefano M 91 Reputation points
2021-03-24T11:56:34.253+00:00

I have a Observablecollection made up of some certain elements (groups). Each element inside it has another ObservableCollection with other elements. I need this to create a CollectionView with groups. If I try to delete a group from my collection, I keep seeing it in the collectionview. Do I need to update the collectionView in some way? If I try to delete a single element within the group, I see the changes, while if I delete the group, in reality the group continues to exist

Group group = MyCollection.Where(x => x.Name == "NameGroup".First(); 
MyCollection.Remove(group);
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,378 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,966 Reputation points
    2021-03-24T14:39:11.957+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Hi, StefanoM-9408. How did you set data binding with the grouped collectionView? I created a basic demo to test the function, it works fine. Here is the related code, you could refer to it.

    Check the code:

       //page.xaml  
       <StackLayout Margin="20">  
           <Button Clicked="Button_Clicked"/>  
           <CollectionView ItemsSource="{Binding DataCollection}"  
                               IsGrouped="true">  
               ...  
           </CollectionView>  
       </StackLayout>  
       //page.xaml.cs  
       public partial class Page1 : ContentPage  
       {  
           TestViewModel viewModel;  
           public Page1()  
           {  
               InitializeComponent();  
               viewModel = new TestViewModel();  
               BindingContext = viewModel;  
           }  
         
           private void Button_Clicked(object sender, EventArgs e)  
           {  
               var itemCollection = viewModel.DataCollection.Where(x => x.Name == "group_1").First();  
               if (itemCollection != null)  
               {  
                   viewModel.DataCollection.Remove(itemCollection);  
               }  
           }  
       }  
    

    The related Model class and ViewModel class:

       public class TestModel : INotifyPropertyChanged  
       {  
           private string content;  
           public string Content  
           {  
               get  
               {  
                   return content;  
               }  
               set  
               {  
                   if (content != value)  
                   {  
                       content = value;  
                       NotifyPropertyChanged();  
                   }  
               }  
           }  
         
           protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")  
           {  
               PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
           }  
         
           public event PropertyChangedEventHandler PropertyChanged;  
       }  
         
       public class TestModelGroup : ObservableCollection<TestModel>, INotifyPropertyChanged  
       {  
           public TestModelGroup(string name, List<TestModel> testModel) : base(testModel)  
           {  
               Name = name;  
           }  
         
           public override string ToString()  
           {  
               return Name;  
           }  
         
           private string name;  
           public string Name  
           {  
               get  
               {  
                   return name;  
               }  
               set  
               {  
                   if (name != value)  
                   {  
                       name = value;  
                       NotifyPropertyChanged();  
                   }  
               }  
           }  
         
           protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")  
           {  
               PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
           }  
           public event PropertyChangedEventHandler PropertyChanged;  
       }  
         
       public class TestViewModel  
       {  
           public ObservableCollection<TestModelGroup> DataCollection { get; set; }  
         
           public TestViewModel(bool emptyGroups = false)  
           {  
               DataCollection = new ObservableCollection<TestModelGroup>();  
               CreateDataCollection();  
           }  
         
           private void CreateDataCollection()  
           {  
               //add the data  
           }  
       }  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.