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.