Beispiel #1
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 #2
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);
                }
            }
        }