question

StefanoM-9408 avatar image
0 Votes"
StefanoM-9408 asked JarvanZhang-MSFT commented

Delete a group from a collectionview from observableCollection

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);


dotnet-xamarinforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered JarvanZhang-MSFT commented

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.


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Previously my BindingContext was

 this.BindingContext = this

now:

 Group groupbindig;
         public DiaryPage()
         {
             //this.BindingContext = this;
             BindingContext = groupbindig;
     

        //other code
              }
    
 public class Group : ObservableCollection<Diary>
         {
             public string Name { get; private set; }
    
             public Group(string name, ObservableCollection<Diary> icon) : base(icon)
             {
                 Name = name;
             }
    
         }

However, it still does not work properly. I manage to delete all elements within the group, but the group with the title remains with me








0 Votes 0 ·

ADD group:

     HumorGroup group = new HumorGroup(Mese + " " + hd.Dt.Year.ToString(), new ObservableCollection<HumorDiary>());
        
     if (!TotHumor.Contains(group))
     {
        TotHumor.Add(group);
     }

REMOVE group:


    HumorGroup hgroup = TotHumor.Where(x => x.Name == month + " " + time.Year.ToString()).First();
            
         TotHumor.Remove(hgroup)
0 Votes 0 ·

Please try to use a model class to wrap the group collection as below.

TestViewModel groupbindig;
public DiaryPage()
{
    groupbindig = new TestViewModel();
    BindingContext = groupbindig;
}

//
public class Group : ObservableCollection<Diary>
{
    public string Name { get; private set; }

    public Group(string name, ObservableCollection<Diary> icon) : base(icon)
    {
        Name = name;
    }
}

public class TestViewModel
{
    public ObservableCollection<Diary> DataCollection { get; set; }

    public TestViewModel()
    {
        DataCollection = new ObservableCollection<Diary>();
        CreateDataCollection();
    }

    private void CreateDataCollection()
    {
        //add the data
    }
}

Here is related doc about CollectionView grouping, you could refer to it.
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview/grouping

0 Votes 0 ·