internal override void OnSelectedItemChanged(object oldSelectedItem, object selectedItem) { if (_modifyingSelectionInternally) { return; } if (IsSelectionMultiple) { var items = GetItems(); if (selectedItem == null || items.Contains(selectedItem)) { object[] removedItems = null; object[] addedItems = null; try { _modifyingSelectionInternally = true; removedItems = SelectedItems.Except(selectedItem).ToObjectArray(); var isRealSelection = selectedItem != null || items.Contains(null); addedItems = SelectedItems.Contains(selectedItem) || !isRealSelection ? new object[0] : new[] { selectedItem }; SelectedItems.Clear(); if (isRealSelection) { SelectedItems.Add(selectedItem); } } finally { _modifyingSelectionInternally = false; } //Invoke event after resetting flag, in case callbacks in user code modify the collection if (addedItems.Length > 0 || removedItems.Length > 0) { InvokeSelectionChanged(removedItems, addedItems); } } else { SelectedItem = oldSelectedItem; } } else { SelectedItems.Clear(); SelectedItems.Add(selectedItem); base.OnSelectedItemChanged(oldSelectedItem, selectedItem); } }
private void UpdateElements(ISet <FrameworkElement> selectedItems) { var added = selectedItems.Except(SelectedItems); var removed = SelectedItems.Except(selectedItems); foreach (var element in added) { element.SetValue(TargetSelectionProperty, true); } foreach (var element in removed) { element.SetValue(TargetSelectionProperty, false); } SelectedItems = selectedItems; }
private void NewSelection(T selectedItem) { if (multiModifierDown) { if (SelectedItems.Add(selectedItem)) { Selection?.Invoke(this, new ItemSelectionEventArgs <T>(selectedItem, SelectionType.Selected)); } } else { bool alreadySelected = SelectedItems.Contains(selectedItem); var toRemove = SelectedItems.Except(selectedItem).ToArray(); foreach (var b in toRemove) { Deselect(b); } if (!alreadySelected) { SelectedItems.Add(selectedItem); Selection?.Invoke(this, new ItemSelectionEventArgs <T>(selectedItem, SelectionType.Selected)); } } }