private void SourceSelection_CollectionChanged([NotNull] object sender, [NotNull] NotifyCollectionChangedEventArgs e)
            {
                if (IsUpdating)
                {
                    return;
                }

                IsUpdating = true;

                try
                {
                    _selector.CommitEdit();

                    var selectedItems = _selector.GetSelectedItems();

                    var itemsToSelect   = e.NewItems;
                    var itemsToDeselect = e.OldItems;

                    switch (e.Action)
                    {
                    case NotifyCollectionChangedAction.Reset:
                        var sourceSelection = (IList)sender;
                        _selector.SynchronizeWithSource(sourceSelection);
                        break;

                    case NotifyCollectionChangedAction.Add:
                        if ((selectedItems.Count == 0) && (itemsToSelect.Count == 1))
                        {
                            _selector.SelectSingleItem(itemsToSelect);
                        }
                        else
                        {
                            _selector.AddItemsToSelection(itemsToSelect);
                        }
                        break;

                    case NotifyCollectionChangedAction.Remove:
                        selectedItems.RemoveRange(itemsToDeselect);
                        break;

                    case NotifyCollectionChangedAction.Replace:
                        if ((selectedItems.Count == 1) && (itemsToSelect.Count == 1))
                        {
                            _selector.SelectSingleItem(itemsToSelect);
                        }
                        else
                        {
                            selectedItems.RemoveRange(itemsToDeselect);
                            _selector.AddItemsToSelection(itemsToSelect);
                        }
                        break;
                    }
                }
                finally
                {
                    IsUpdating = false;
                }
            }
Ejemplo n.º 2
0
        private static void SynchronizeWithSource([NotNull] this Selector selector, [NotNull, ItemCanBeNull] IList sourceSelection)
        {
            Contract.Requires(selector != null);
            Contract.Requires(sourceSelection != null);

            var selectedItems = selector.GetSelectedItems();

            if ((selectedItems.Count == sourceSelection.Count) && sourceSelection.All(selectedItems.Contains))
            {
                return;
            }

            selector.CommitEdit();

            // Clear the selection.
            selector.SelectedIndex = -1;

            if (sourceSelection.Count == 1)
            {
                selector.SelectSingleItem(sourceSelection);
            }
            else
            {
                selector.AddItemsToSelection(sourceSelection);
            }
        }
        private static void SynchronizeWithSource(this Selector selector, IList sourceSelection)
        {
            var selectedItems = selector.GetSelectedItems();

            if ((selectedItems.Count == sourceSelection.Count) && sourceSelection.Cast <object>().All(selectedItems.Contains))
            {
                return;
            }

            selector.CommitEdit();

            // Clear the selection.
            selector.SelectedIndex = -1;

            if (sourceSelection.Count == 1)
            {
                selector.SelectSingleItem(sourceSelection);
            }
            else
            {
                selector.AddItemsToSelection(sourceSelection);
            }
        }