private void CreateDeltaSelectionChange(List<object> unselectedItems, List<object> selectedItems) { for (int i = 0; i < _toDeferSelect.Count; i++) { object o = _toDeferSelect[i]; // If defered selected item exis in Items - move it to _toSelect if (_owner.Items.Contains(o)) { _toSelect.Add(o); _toDeferSelect.Remove(o); i--; } } if (_toUnselect.Count > 0 || _toSelect.Count > 0) { if (_owner._selectedItems.UsesItemHashCodes) { // Step 1: Keep current selection temp storage InternalSelectedItemsStorage currentSelectedItems = new InternalSelectedItemsStorage(_owner._selectedItems); // Step 2: Clear the selection storage _owner._selectedItems.Clear(); // STep 3: Process the items that need to be unselected foreach (object o in _toUnselect) { _owner.ItemSetIsSelected(o, false); if (currentSelectedItems.Contains(o)) { unselectedItems.Add(o); } } // Step 4: Add back items from the temp storage that are not in _toUnselect foreach (object o in currentSelectedItems) { if (!_toUnselect.Contains(o)) { _owner._selectedItems.Add(o); } } } else { foreach (object o in _toUnselect) { _owner.ItemSetIsSelected(o, false); if (_owner._selectedItems.Contains(o)) { _owner._selectedItems.Remove(o); unselectedItems.Add(o); } } } foreach (object o in _toSelect) { _owner.ItemSetIsSelected(o, true); if (!_owner._selectedItems.Contains(o)) { _owner._selectedItems.Add(o); selectedItems.Add(o); } } } }
private void UpdateSelectedItems() { // Update SelectedItems. We don't want to invalidate the property // because that defeats the ability of bindings to be able to listen // for collection changes on that collection. Instead we just want // to add all the items which are not already in the collection. // Note: This is currently only called from SelectionChanger where SC.IsActive will be true. // If this is ever called from another location, ensure that SC.IsActive is true. Debug.Assert(SelectionChange.IsActive, "SelectionChange.IsActive should be true"); IList userSelectedItems = SelectedItemsImpl; if (userSelectedItems != null) { InternalSelectedItemsStorage userSelectedItemsTable = new InternalSelectedItemsStorage(userSelectedItems.Count); userSelectedItemsTable.UsesItemHashCodes = _selectedItems.UsesItemHashCodes; for (int i = 0; i < userSelectedItems.Count; i++) { object userSelectedItem = userSelectedItems[i]; if (_selectedItems.Contains(userSelectedItem) && !userSelectedItemsTable.Contains(userSelectedItem)) { // cache the user's selected items into a table with O(1) lookup. userSelectedItemsTable.Add(userSelectedItem); } else { // Remove each thing that is in SelectedItems that's not in _selectedItems. // Remove all duplicate items from userSelectedItems userSelectedItems.RemoveAt(i); i--; } } // Add to SelectedItems everything missing that's in _selectedItems. foreach (object selectedItem in _selectedItems) { if (!userSelectedItemsTable.Contains(selectedItem)) { userSelectedItems.Add(selectedItem); } } } }