Beispiel #1
0
        public void SelectSourceCollection(IEnumerable <object> sourceCollection)
        {
            using (SelectionHandlingScope)
            {
                if (MultipleSelection)
                {
                    foreach (var source in sourceCollection)
                    {
                        if (CurrentSelectionCollection.FindBySource(source, out _) == false)
                        {
                            if (Advisor.TryCreateSelection(source, false, out var selection) && selection.IsEmpty == false)
                            {
                                CurrentSelectionCollection.Select(selection);

                                SetItemSelected(selection.Item, true);
                            }
                        }
                    }

                    SelectFirst();
                }
                else
                {
                    SelectSourceSafe(sourceCollection.FirstOrDefault());
                }
            }
        }
Beispiel #2
0
        private void OnItemCollectionSourceChangedSafe(NotifyCollectionChangedEventArgs e)
        {
            VerifySafe();

            if (e.Action == NotifyCollectionChangedAction.Move)
            {
            }
            else if (e.Action == NotifyCollectionChangedAction.Reset)
            {
                if (SupportsIndex)
                {
                    if (MultipleSelection)
                    {
                        CurrentSelectionCollection.UpdateIndicesSources();
                    }

                    if (CurrentSelectedIndex != -1)
                    {
                        var source = GetSource(CurrentSelectedIndex);

                        Sync(source != null ? CurrentSelection.WithSource(source) : CurrentSelection.WithIndex(-1));
                    }
                }
            }
            else
            {
                if (e.NewItems != null)
                {
                    foreach (var source in e.NewItems)
                    {
                        if (IsSourceSelected(source))
                        {
                            SelectSourceSafe(source);
                        }
                    }
                }

                if (e.OldItems != null)
                {
                    foreach (var source in e.OldItems)
                    {
                        if (IsSourceInSelection(source))
                        {
                            UnselectSourceSafe(source);
                        }
                    }
                }
            }

            SyncIndex(false);
            EnsureSelection();
        }
Beispiel #3
0
        private bool IsSourceInSelection(object source)
        {
            if (source == null)
            {
                return(false);
            }

            if (ReferenceEquals(source, CurrentSelectedSource))
            {
                return(true);
            }

            return(MultipleSelection && CurrentSelectionCollection.ContainsSource(source));
        }
Beispiel #4
0
        private bool IsItemInSelection(TItem item)
        {
            if (item == null)
            {
                return(false);
            }

            if (ReferenceEquals(item, CurrentSelectedItem))
            {
                return(true);
            }

            return(MultipleSelection && CurrentSelectionCollection.ContainsItem(item));
        }
        private void UnselectAllSafe()
        {
            VerifySafe();

            foreach (var selection in CurrentSelectionCollection)
            {
                SetItemSelected(selection.Item, false);
            }

            CurrentSelectionCollection.Clear();
            ApplySelectionSafe(Selection <TItem> .Empty);

            CurrentSelectionCollection.IsInverted = false;
        }
Beispiel #6
0
        private void SelectCore(Selection <TItem> selection)
        {
            CurrentSelectionCollection.Select(selection);

            if (MultipleSelection)
            {
                SelectFirst();

                return;
            }

            ApplySelection(selection);

            Version++;
        }
Beispiel #7
0
        private void SyncIndex(int itemIndex)
        {
            if (itemIndex == CurrentSelectedIndex)
            {
                return;
            }

            if (MultipleSelection)
            {
                if (CurrentSelectionCollection.FindByIndex(itemIndex, out var selection))
                {
                    if (itemIndex != selection.Index)
                    {
                        CurrentSelectionCollection.UpdateSelection(itemIndex, selection);
                    }
                }
            }

            Sync(CurrentSelection.WithIndex(itemIndex));
        }
        private bool UnselectItemCore(TItem item, bool force)
        {
            CurrentSelectionCollection.UnselectItem(item);

            if (ReferenceEquals(CurrentSelectedItem, item))
            {
                if (MultipleSelection)
                {
                    SelectFirst();

                    return(true);
                }

                return(SelectItemSafe(null));
            }

            EnsureSelection();

            return(true);
        }
        private bool UnselectIndexCore(int index, bool force)
        {
            CurrentSelectionCollection.UnselectIndex(index);

            if (SelectedIndex == index)
            {
                if (MultipleSelection)
                {
                    SelectFirst();

                    return(true);
                }

                return(SelectIndexSafe(-1));
            }

            EnsureSelection();

            return(true);
        }
        private bool UnselectValueCore(object value, bool force)
        {
            CurrentSelectionCollection.UnselectValue(value);

            if (ReferenceEquals(SelectedValue, value))
            {
                if (MultipleSelection)
                {
                    SelectFirst();

                    return(true);
                }

                return(SelectValueSafe(null));
            }

            EnsureSelection();

            return(true);
        }
        private bool UnselectSourceCore(object source, bool force)
        {
            CurrentSelectionCollection.UnselectSource(source);

            if (ReferenceEquals(SelectedSource, source))
            {
                if (MultipleSelection)
                {
                    SelectFirst();

                    return(true);
                }

                return(SelectSourceSafe(null));
            }

            EnsureSelection();

            return(true);
        }
        public void UnselectSourceCollection(IEnumerable <object> sourceCollection)
        {
            using (SelectionHandlingScope)
            {
                foreach (var source in sourceCollection)
                {
                    if (CurrentSelectionCollection.FindBySource(source, out var selection))
                    {
                        CurrentSelectionCollection.Unselect(selection);

                        if (Advisor.TryGetItem(source, false, out var item))
                        {
                            SetItemSelected(item, false);
                        }
                    }
                }

                SelectFirst();
            }

            EnsureSelection();
        }
Beispiel #13
0
        private void OnItemDetachedSafe([UsedImplicitly] int index, TItem item)
        {
            VerifySafe();

            if (HasSource)
            {
                if (IsLocked(item) == false)
                {
                    var source = GetSource(item);

                    if (MultipleSelection)
                    {
                        if (CurrentSelectionCollection.FindBySource(source, out var selection))
                        {
                            if (ReferenceEquals(item, selection.Item))
                            {
                                CurrentSelectionCollection.UpdateSelection(selection.Index, selection.WithItem(null));
                            }
                        }
                    }

                    if (ReferenceEquals(source, CurrentSelectedSource))
                    {
                        if (ReferenceEquals(item, CurrentSelectedItem))
                        {
                            Sync(CurrentSelection.WithItem(null));
                        }
                    }
                }
            }
            else
            {
                if (IsVirtualizing == false)
                {
                    UnselectItemSafe(item);
                }
            }
        }
Beispiel #14
0
        private void SelectFirst()
        {
            ApplySelection(CurrentSelectionCollection.Count > 0 ? CurrentSelectionCollection.First() : Selection <TItem> .Empty);

            Version++;
        }
Beispiel #15
0
        private void OnItemAttachedSafe(int index, TItem item)
        {
            VerifySafe();

            var itemSelected = GetIsItemSelected(item);

            if (HasSource)
            {
                // TODO Implement Sync mechanism to determine if item source is actually selected.
                // TODO For example ListViewItem can have two way binding on IsSelected property to its DataContext and DataContext changes its selection state.
                // TODO In this case selected item source will stay in SelectionCollection and code below will behave incorrectly.
                // TODO Sync mechanism could be implemented through event in Selector control. As another option check if Item Selection property is bound to Data Context
                // TODO and perform immediate update on the binding to update Item IsSelected property.

                var source = GetSource(item);

                if (MultipleSelection)
                {
                    if (CurrentSelectionCollection.FindBySource(source, out var selection))
                    {
                        if (ReferenceEquals(item, selection.Item) == false)
                        {
                            CurrentSelectionCollection.UpdateSelection(selection.Index, selection.WithItem(item));
                        }
                    }
                }

                if (ReferenceEquals(source, CurrentSelectedSource))
                {
                    if (ReferenceEquals(item, CurrentSelectedItem) == false)
                    {
                        Sync(CurrentSelection.WithItem(item));
                    }
                }

                {
                    var itemSourceInSelection = IsSourceInSelection(source);

                    if (itemSourceInSelection && itemSelected == false)
                    {
                        if (CanSelectItem(item))
                        {
                            SetItemSelected(item, true);
                        }
                        else
                        {
                            UnselectItemSafe(item);
                        }
                    }

                    if (itemSourceInSelection == false && itemSelected)
                    {
                        SetItemSelected(item, false);
                    }
                }
            }
            else
            {
                if (SupportsIndex)
                {
                    var selectedIndex = CurrentSelectedIndex;

                    if (selectedIndex != -1)
                    {
                        var itemIndex = index == -1 ? GetIndexOfItem(item) : index;

                        if (MultipleSelection)
                        {
                            if (CurrentSelectionCollection.FindByIndex(index, out var selection))
                            {
                                if (ReferenceEquals(item, selection.Item) == false)
                                {
                                    CurrentSelectionCollection.UpdateSelection(selection.Index, selection);
                                }
                            }
                        }

                        if (selectedIndex == itemIndex)
                        {
                            if (ReferenceEquals(item, CurrentSelectedItem) == false)
                            {
                                Sync(CurrentSelection.WithItem(item));
                            }
                        }
                    }
                }

                var itemInSelection = IsItemInSelection(item);

                if (itemInSelection && itemSelected == false)
                {
                    if (CanSelectItem(item))
                    {
                        SetItemSelected(item, true);
                    }
                    else
                    {
                        UnselectItemSafe(item);
                    }
                }

                if (itemSelected && itemInSelection == false)
                {
                    SelectItemSafe(item);
                }
            }
        }