Caliburn.Micro icon indicating copy to clipboard operation
Caliburn.Micro copied to clipboard

BindableCollection AddRange/RemoveRange NewItems/OldItems are now set

Open Yinimi opened this issue 5 years ago • 9 comments

When adding objects to a BindableCollection the e.NewItems in the CollectionChanged event used to be empty.

Current version: image

Fixed version: image

Code snipped: Program.txt

Yinimi avatar May 22 '19 09:05 Yinimi

Looks good, thanks, just need to do some legacy research. This is one of those changes that makes me wonder why it hasn't been done before.

nigel-sampson avatar May 30 '19 04:05 nigel-sampson

be aware of dotnet/wpf#1887

tibel avatar Jan 22 '20 06:01 tibel

Just in case it might be useful: some collections (such as ListCollectionView) do not support ranged actions and throw. It works with Reset.

PhyxionNL avatar Aug 18 '20 14:08 PhyxionNL

I rebased my fork and updated the code

Yinimi avatar Jun 09 '21 20:06 Yinimi

@Yinimi Code looks good. Need to do some testing this weekend

vb2ae avatar Jun 10 '21 11:06 vb2ae

@KasperSK it is testing out ok. What do you think about merging it in?

vb2ae avatar Jun 12 '21 19:06 vb2ae

@vb2ae Could we test the case mentioned in dotnet/wpf#1887? Seems bad if we could get runtime exceptions when running WPF.

KasperSK avatar Jun 13 '21 10:06 KasperSK

Tested with this code

    protected override async Task OnActivateAsync(CancellationToken cancellationToken)
    {
        Numbers = new BindableCollection<int>();
        Numbers.CollectionChanged += Numbers_CollectionChanged;
        for (int x = 0; x < 10000; x++)
        {
            Numbers.Add(x);
        }
    }

    public void btnChangeCollection()
    {
        Random rnd = new Random();
        var lst = new List<int>();
        for (int x = 0; x < 100; x++)
        {
            lst.Add(Numbers[x]);
        }

        Numbers.RemoveRange(lst);
        var lstToAdd = new List<int>();
        for (int z = 0; z < 100; z++)
        {
            lstToAdd.Add(rnd.Next(200000));
        }

        Numbers.AddRange(lstToAdd);
    }

Numbers was bound to a listbox. AddRange and RemoveRange throw exceptions like @tibel mentioned

image

vb2ae avatar Jun 13 '21 19:06 vb2ae

Looks like we should not merge this before WPF has fixed the issue then.

KasperSK avatar Jun 14 '21 07:06 KasperSK