void OnItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { var oldIndex = SelectedIndex; var newIndex = SelectedIndex = SelectedIndex.Clamp(-1, Items.Count - 1); // If the index has not changed, still need to change the selected item if (newIndex == oldIndex) { UpdateSelectedItem(newIndex); } }
void ItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Add: AddItems(e); break; case NotifyCollectionChangedAction.Remove: RemoveItems(e); break; default: // Move, replace, reset ResetItems(); break; } SelectedIndex = SelectedIndex.Clamp(-1, Items.Count - 1); UpdateSelectedItem(); }
void OnItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { SelectedIndex = SelectedIndex.Clamp(-1, Items.Count - 1); }