@@ -49,8 +49,8 @@ public ReadOnlyObservableGroupedCollection(IEnumerable<IGrouping<TKey, TValue>>
49
49
50
50
private void OnSourceCollectionChanged ( object ? sender , NotifyCollectionChangedEventArgs e )
51
51
{
52
- // Even if the NotifyCollectionChangedEventArgs allows multiple items, the actual implementation is only
53
- // reporting the changes one by one. We consider only this case for now.
52
+ // Even if NotifyCollectionChangedEventArgs allows multiple items, the actual implementation
53
+ // is only reporting the changes one by one. We consider only this case for now.
54
54
if ( e . OldItems ? . Count > 1 || e . NewItems ? . Count > 1 )
55
55
{
56
56
static void ThrowNotSupportedException ( )
@@ -65,28 +65,33 @@ static void ThrowNotSupportedException()
65
65
ThrowNotSupportedException ( ) ;
66
66
}
67
67
68
- ObservableGroup < TKey , TValue > ? newItem = e . NewItems ? . Cast < ObservableGroup < TKey , TValue > > ( ) ? . FirstOrDefault ( ) ;
69
-
70
- if ( newItem is null )
71
- {
72
- return ;
73
- }
74
-
75
68
switch ( e . Action )
76
69
{
77
- case NotifyCollectionChangedAction . Add :
78
- Items . Insert ( e . NewStartingIndex , new ReadOnlyObservableGroup < TKey , TValue > ( newItem ) ) ;
70
+ case NotifyCollectionChangedAction . Add or NotifyCollectionChangedAction . Replace :
71
+
72
+ // We only need to find the new item if the operation is either add or remove. In this
73
+ // case we just directly find the first item that was modified, or throw if it's not present.
74
+ // This normally never happens anyway - add and replace should always have a target element.
75
+ ObservableGroup < TKey , TValue > newItem = e . NewItems ! . Cast < ObservableGroup < TKey , TValue > > ( ) . First ( ) ;
76
+
77
+ if ( e . Action == NotifyCollectionChangedAction . Add )
78
+ {
79
+ Items . Insert ( e . NewStartingIndex , new ReadOnlyObservableGroup < TKey , TValue > ( newItem ) ) ;
80
+ }
81
+ else
82
+ {
83
+ Items [ e . OldStartingIndex ] = new ReadOnlyObservableGroup < TKey , TValue > ( newItem ) ;
84
+ }
85
+
79
86
break ;
80
87
case NotifyCollectionChangedAction . Move :
88
+
81
89
// Our inner Items list is our own ObservableCollection<ReadOnlyObservableGroup<TKey, TValue>> so we can safely cast Items to its concrete type here.
82
90
( ( ObservableCollection < ReadOnlyObservableGroup < TKey , TValue > > ) Items ) . Move ( e . OldStartingIndex , e . NewStartingIndex ) ;
83
91
break ;
84
92
case NotifyCollectionChangedAction . Remove :
85
93
Items . RemoveAt ( e . OldStartingIndex ) ;
86
94
break ;
87
- case NotifyCollectionChangedAction . Replace :
88
- Items [ e . OldStartingIndex ] = new ReadOnlyObservableGroup < TKey , TValue > ( newItem ) ;
89
- break ;
90
95
case NotifyCollectionChangedAction . Reset :
91
96
Items . Clear ( ) ;
92
97
break ;
0 commit comments