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

[UWP] NotifyCollectionChangedAction.Add behaves differently while working with custom ObservableCollection #7993

Closed
sethuramkumar opened this issue Dec 6, 2022 · 1 comment
Labels
appModel-UWP Exclusive to WinUI 2 UWP apps bug Something isn't working needs-author-feedback Asked author to supply more information. needs-repro needs-triage Issue needs to be triaged by the area owners team-Controls Issue for the Controls team

Comments

@sethuramkumar
Copy link

Describe the bug

Exception throws when using AddRange() for custom observable collection and using OnCollectionChanged as below:

OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add,addedItems));

Same scenario works fine when OnCollectionChanged was used as below:

OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));

Created a custom class based on Observable Collection and declare a method "AddRange()" as below .

public class ObservableRangeCollection<TItem> : ObservableCollection<TItem>
{
    public void AddRange(IEnumerable<TItem> list)
    {
        if (list == null)
            throw new ArgumentNullException(nameof(list));

        IList addedItems = new List<TItem>();
        IList OldList = Items as List<TItem>;
        foreach (TItem item in list)
        {
            Items.Add(item);
            addedItems.Add(item);
        }
        // Exception case
        OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add,addedItems));

        // working case
      //  OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    }
}


Code used in Model:

public class Model
{
    public string Country { get; set; }

    public Model(string country)
    {
        Country = country;
    }
}

Code Used in ViewModel

public class ViewModel : INotifyPropertyChanged
{

    private ObservableRangeCollection<Model> _countries;

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public ObservableRangeCollection<Model> Countries
    {
        get { return _countries; }
        set
        {
            _countries = value;
            OnPropertyChanged("Employees");
        }
    }

    public ViewModel() 
    {
        Countries= new ObservableRangeCollection<Model>();
    }
}

Code used in Main Page:

<StackPanel Orientation="Vertical">
    <ListView Width="300" Height="150" Margin="3" ItemsSource="{Binding Countries}" BorderBrush=
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Country}"/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    <Button Content="Add Range" Click="Button_Click" Height="30" HorizontalAlignment="Center"/>
</StackPanel>

Code Behind:

private void Button_Click(object sender, RoutedEventArgs e)
{
    List<Model> list = new List<Model>
    {
        new Model("Austria"),new Model("Africa"),new Model("China")
    };
    viewModel.Countries.AddRange(list);
}

Why does OnCollectionChanged with NotifyCollectionChangedAction.Reset as parameter has to be raised for Adding items in the collection?

Note: Sample works fine when OnCollectionChanged() raised with NotifyCollectionChangedAction.Add, addedItems and starting index as parameter.
Exception occurs only when using the OnCollectionChanged with NotifyCollectionChangedAction.Add and addedItems.

Steps to reproduce the bug

  1. Declare Custom class based on ObservableCollection.
  2. Declare a method to Add range of items and then raise OnCollectionChanged() with NotifyCollecitonChangedAction.Add and addedItems as parameter.

Expected behavior

Exception should not be thrown, items should be properly added in the collection.

Screenshots

Exception Image:
image

NuGet package version

None

Windows version

Windows 11 (21H2): Build 22000

Additional context

No response

@sethuramkumar sethuramkumar added the bug Something isn't working label Dec 6, 2022
@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Dec 6, 2022
@bpulliam bpulliam added appModel-UWP Exclusive to WinUI 2 UWP apps and removed needs-triage Issue needs to be triaged by the area owners labels Jul 7, 2023
@bpulliam bpulliam added the team-Controls Issue for the Controls team label Aug 9, 2023
@DmitriyKomin DmitriyKomin added needs-repro needs-author-feedback Asked author to supply more information. labels Feb 26, 2024
@michael-hawker
Copy link
Collaborator

@DmitriyKomin tags were added but no comments/info about what is required here?

Would this maybe be related to what needs to be done to support dotnet/runtime#18087 as well?

@microsoft-github-policy-service microsoft-github-policy-service bot added needs-triage Issue needs to be triaged by the area owners and removed no-recent-activity labels Jul 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
appModel-UWP Exclusive to WinUI 2 UWP apps bug Something isn't working needs-author-feedback Asked author to supply more information. needs-repro needs-triage Issue needs to be triaged by the area owners team-Controls Issue for the Controls team
Projects
None yet
Development

No branches or pull requests

4 participants