Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ListView doesn't handle NotifyCollectionChangedAction.Replace with different number of items in old and new. #9607

Open
euju-ms opened this issue May 3, 2024 · 1 comment
Labels
area-Lists ListView, GridView, ListBox, etc bug Something isn't working team-Controls Issue for the Controls team

Comments

@euju-ms
Copy link

euju-ms commented May 3, 2024

Describe the bug

When a ListView databinds to a collection that implements INotifyCollectionChanged, and the collection invokes a CollectionChanged event with NotifyCollectionChangedAction.Replace with different number of items in newItems and oldItems, the ListView does not update.

I uploaded a simple test app here: https://github.com/euju-ms/CollectionChangedReplaceBug

Essentially, when my testCollection gains an item, it fires a replace event
E.g. [A, B] => [A, B, C]

In this case, ListView should update its visual from showing A, B to showing A, B, C, but it doesn't change in visual at all.
Replace event with same number of items in newItems and oldItems seem to work fine.

`
public void AddByReplace(string new Item)
{
_list.Add(newItem); // _list is a List.

CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(
    NotifyCollectionChangedAction.Replace,
    _list.ToArray(),
    _list.GetRange(0, _list.Count - 1),
    0));

}
`

<ListView Header="TestCollection" ItemsSource="{x:Bind testCollection, Mode=OneWay}" ItemTemplate="{StaticResource StringDataTemplate}" /> <!-- StringDataTemplate is just a TextBlock -->

I couldn't find any restriction on "Replace" CollectionChanged event needing to have the same number of items in newItems and oldItems, so I'm filing this as a bug here.

E.g. NotifyCollectionChangedEventArgs constructor for Replace does not check for the number of items.

Steps to reproduce the bug

  1. Get the source code from https://github.com/euju-ms/CollectionChangedReplaceBug
  2. Run the application from VisualStudio as a packaged app.
  3. Click on the button with Replace (2->3)
  4. Notice that items on the left don't change (below TestCollection).

Expected behavior

Items on the left (below "TestCollection") should change whenever "Replace (2->3)" button is clicked.

Screenshots

On startup:
image

Clicking "Replace (2 -> 3)"
Actual:
image

Expected result is that you see three items below TestCollection, "hello", "world", "New Text 0".

NuGet package version

None

Windows version

No response

Additional context

No response

@euju-ms euju-ms added the bug Something isn't working label May 3, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label May 3, 2024
Copy link

github-actions bot commented May 3, 2024

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

@codendone codendone added area-Lists ListView, GridView, ListBox, etc team-Controls Issue for the Controls team and removed needs-triage Issue needs to be triaged by the area owners labels May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Lists ListView, GridView, ListBox, etc bug Something isn't working team-Controls Issue for the Controls team
Projects
None yet
Development

No branches or pull requests

2 participants