dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.26k stars 1.76k forks source link

MAUI Grouped ListView Remove Item Causes Error when more than one Group item exists #26015

Open PBlaa opened 5 hours ago

PBlaa commented 5 hours ago

Description

I have a grouped list in C# MAUI MVVC, destination is Andoid

public class GroupData : ObservableCollection<DetailData>
{
    public string Name { get; private set; }
    public GroupData(string name, ObservableCollection<DetailData> d) : base(d) 
              { Name = name; }
}

public class DetailData
{
    public int Nr { get; set; }
    public string Detail { get; set; }
}

Test data are added like this

public ObservableCollection<GroupData> GetData()
{
    ObservableCollection<GroupData> data = new ObservableCollection<GroupData>();

    data.Add(new GroupData("Group1", new ObservableCollection<DetailData>
    {
        new DetailData { Nr = 1, Detail = "Test 1" },
        new DetailData { Nr = 2, Detail = "Test 2" }
    }));

    data.Add(new GroupData("Group2", new ObservableCollection<DetailData>
    {
        new DetailData { Nr = 3, Detail = "Test 3" },
        new DetailData { Nr = 4, Detail = "Test 4" }
    }));

    return data;
}

Main Page View Model has the bindable collection

public ObservableCollection<GroupData> GroupData { get; } = new ObservableCollection<GroupData>();

and an event on a button when showing the list. Clicking on it should remove the selected item.

[RelayCommand]
private async Task ButtonOk(DetailData data)
{
    foreach (var item in GroupData)
    {
        foreach (var child in item)
        {
            if (child.Nr == data.Nr)
            {
                item.Remove(child);
                break;
            }
        }
    }
}

Data are shown with this

<RefreshView Command="{Binding RefreshCommand}" IsRefreshing="{Binding IsBusy, Mode=TwoWay}">
    <ScrollView>
        <Grid>
            <ListView ItemsSource="{Binding GroupData}" 
                      IsGroupingEnabled="True"
                      HasUnevenRows="True">
                <ListView.GroupHeaderTemplate>
                    <DataTemplate x:DataType="daten:GroupData">
                        <ViewCell>
                            <Label Text="{Binding Name}"/>
                        </ViewCell>
                    </DataTemplate>
                </ListView.GroupHeaderTemplate>
                <ListView.ItemTemplate>
                    <DataTemplate x:DataType="daten:DetailData">
                        <ViewCell>
                            <Grid >
                                <Label Text="{Binding Detail}"/>
                                <ImageButton Source="ok.jfif"
                                    Command="{Binding Source={RelativeSource AncestorType={x:Type local:MainPageViewModel}}, 
                                    Path=ButtonOkCommand}" 
                                    CommandParameter="{Binding .}"/>
                            </Grid>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    </ScrollView>
</RefreshView>

Now, when adding ONLY ONE grouped item to the observable collection "Group 1" with the two childs the removing works perfect.

But when adding a SECOND "Group 2" it unfortunately fails with error

Java.Lang.IllegalStateException: 'The specified child already has a parent. You must call removeView() on the child's parent first.'

Steps to Reproduce

see code and description

Link to public reproduction project repository

No response

Version with bug

8.0.93 SR9.3

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

No response

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

remove

<RefreshView >
    <ScrollView>

Relevant log output