private void DeleteItem() { if (SelectedIndex >= 0) { SourceItems.RemoveAt(SelectedIndex); } }
void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Add: if (e.NewStartingIndex < 0) { goto case NotifyCollectionChangedAction.Reset; } for (int i = e.NewItems.Count - 1; i >= 0; i--) { SourceItems.Insert(e.NewStartingIndex, e.NewItems[i]); } break; case NotifyCollectionChangedAction.Remove: foreach (var item in e.OldItems) { SourceItems.RemoveAt(e.OldStartingIndex); } break; case NotifyCollectionChangedAction.Move: for (var i = 0; i < e.OldItems.Count; i++) { var oldi = e.OldStartingIndex; var newi = e.NewStartingIndex; if (e.NewStartingIndex < e.OldStartingIndex) { oldi += i; newi += i; } SourceItems.Move(oldi, newi); } break; case NotifyCollectionChangedAction.Replace: case NotifyCollectionChangedAction.Reset: default: ReloadData(); break; } Device.BeginInvokeOnMainThread(() => List.UpdateLayout()); }
void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Add: if (e.NewStartingIndex < 0) { goto case NotifyCollectionChangedAction.Reset; } // if a NewStartingIndex that's too high is passed in just add the items. // I realize this is enforcing bad behavior but prior to this synchronization // code being added it wouldn't cause the app to crash whereas now it does // so this code accounts for that in order to ensure smooth sailing for the user if (e.NewStartingIndex >= SourceItems.Count) { for (int i = 0; i < e.NewItems.Count; i++) { SourceItems.Add((e.NewItems[i] as BindableObject).BindingContext); } } else { for (int i = e.NewItems.Count - 1; i >= 0; i--) { SourceItems.Insert(e.NewStartingIndex, (e.NewItems[i] as BindableObject).BindingContext); } } break; case NotifyCollectionChangedAction.Remove: for (int i = e.OldItems.Count - 1; i >= 0; i--) { SourceItems.RemoveAt(e.OldStartingIndex); } break; case NotifyCollectionChangedAction.Move: for (var i = 0; i < e.OldItems.Count; i++) { var oldi = e.OldStartingIndex; var newi = e.NewStartingIndex; if (e.NewStartingIndex < e.OldStartingIndex) { oldi += i; newi += i; } SourceItems.Move(oldi, newi); } break; case NotifyCollectionChangedAction.Replace: case NotifyCollectionChangedAction.Reset: default: ClearSizeEstimate(); ReloadData(); break; } Device.BeginInvokeOnMainThread(() => List.UpdateLayout()); }