public void AddGroupInSource_ShouldAddGroup(int sourceInitialItemsCount, int expectedInsertionIndex)
        {
            NotifyCollectionChangedEventArgs collectionChangedEventArgs = null;
            var collectionChangedEventsCount      = 0;
            var isCountPropertyChangedEventRaised = false;
            var itemsList = new[] { 1, 2, 3 };
            var source    = new ObservableGroupedCollection <string, int>();

            for (var i = 0; i < sourceInitialItemsCount; i++)
            {
                source.Add(new ObservableGroup <string, int>($"group {i}", Enumerable.Empty <int>()));
            }

            var readOnlyGroup = new ReadOnlyObservableGroupedCollection <string, int>(source);

            ((INotifyCollectionChanged)readOnlyGroup).CollectionChanged += (s, e) =>
            {
                collectionChangedEventArgs = e;
                collectionChangedEventsCount++;
            };
            ((INotifyPropertyChanged)readOnlyGroup).PropertyChanged += (s, e) => isCountPropertyChangedEventRaised = isCountPropertyChangedEventRaised || e.PropertyName == nameof(readOnlyGroup.Count);

            source.Add(new ObservableGroup <string, int>("Add", itemsList));

            var expectedReadOnlyGroupCount = sourceInitialItemsCount + 1;

            readOnlyGroup.Should().HaveCount(expectedReadOnlyGroupCount);
            readOnlyGroup.Count.Should().Be(expectedReadOnlyGroupCount);
            readOnlyGroup.Last().Key.Should().Be("Add");
            readOnlyGroup.Last().Should().BeEquivalentTo(itemsList, o => o.WithoutStrictOrdering());

            isCountPropertyChangedEventRaised.Should().BeTrue();
            collectionChangedEventArgs.Should().NotBeNull();
            collectionChangedEventsCount.Should().Be(1);
            IsAddEventValid(collectionChangedEventArgs, itemsList, expectedInsertionIndex).Should().BeTrue();
        }
Пример #2
0
        private void AddNewContact()
        {
            var newContact = new Person
            {
                Name = NewContact.Text.Trim(),
            };

            var groupName   = GetGroupName(newContact);
            var targetGroup = _contactsSource.FirstOrDefault(group => group.Key == groupName);

            if (targetGroup is null)
            {
                _contactsSource.Add(new ObservableGroup <string, Person>(groupName, new[] { newContact }));
            }
            else
            {
                targetGroup.Add(newContact);
            }

            NewContact.Text = string.Empty;
        }