Ejemplo n.º 1
0
        private void HandleItemSelection(ItemClickEventArgs args)
        {
            if (SelectionMode != ListViewSelectionMode.None)
            {
                var newSelection = ListViewAdapter.GetItemAt(args.Position);

                switch (SelectionMode)
                {
                case ListViewSelectionMode.Single:
                    var selectedItem = ListViewAdapter.SelectedItems.FirstOrDefault();

                    // Unselect the current item only if a new selection is made or
                    // the option to unselect the current item is activated.
                    if (selectedItem != null && (selectedItem != newSelection || UnselectOnClick))
                    {
                        ListViewAdapter.SetItemSelection(selectedItem, null, false);
                    }

                    if (selectedItem != newSelection)
                    {
                        ListViewAdapter.SetItemSelection(
                            newSelection,
                            (args.View as ItemContainerHolder)?.Child as SelectorItem,
                            true
                            );
                    }

                    SelectedItem = newSelection;

                    if (selectedItem != newSelection)
                    {
                        SelectionChanged?.Invoke(this, new SelectionChangedEventArgs(this, new[] { selectedItem }, new[] { newSelection }));
                    }
                    break;

                case ListViewSelectionMode.Multiple:
                    ListViewAdapter.SetItemSelection(
                        ListViewAdapter.GetItemAt(args.Position),
                        args.View as SelectorItem,
                        !ListViewAdapter.SelectedItems.Contains(newSelection)
                        );
                    break;
                }

                SelectedItems = ListViewAdapter.SelectedItems.ToArray();
            }
        }
Ejemplo n.º 2
0
        private void UpdateSelection(IList <object> newSelection)
        {
            if (ListViewAdapter != null)
            {
                if (!ListViewAdapter.SelectedItems.SequenceEqual(newSelection))
                {
                    var removed = ListViewAdapter
                                  .SelectedItems
                                  .Except(newSelection)
                                  .ToArray();

                    var added = newSelection
                                .Except(ListViewAdapter.SelectedItems)
                                .ToArray();

                    removed.ForEach((object i) => ListViewAdapter.SetItemSelection(i, null, false));
                    added.ForEach((object i) => ListViewAdapter.SetItemSelection(i, null, true));

                    SelectionChanged?.Invoke(this, new SelectionChangedEventArgs(this, removed, added));
                }
            }
        }
Ejemplo n.º 3
0
        partial void OnLoadedPartial()
        {
            SetupItemClickListener();

            if (_adapter != null)
            {
                // The adapter has already been initialized.
                // Re-initializing the adapter would cause the ListView to lose its state (i.e., scroll position) after a reload.
                return;
            }

            _adapter = new ListViewAdapter();
            _adapter.ItemContainerStyle   = ItemContainerStyle;
            _adapter.ItemContainerFactory = () => new ListViewItem()
            {
                ShouldHandlePressed = false
            };
            _adapter.ItemTemplate         = ItemTemplate;
            _adapter.ItemTemplateSelector = ItemTemplateSelector;
            _adapter.ItemClickCommand     = _itemClickCommand;
            _adapter.Header              = Header;
            _adapter.Footer              = Footer;
            _adapter.HeaderTemplate      = HeaderTemplate;
            _adapter.FooterTemplate      = FooterTemplate;
            _adapter.ItemsSource         = ItemsSource;
            _adapter.CustomViewTypeCount = this.CustomViewTypeCount;
            _adapter.GroupStyle          = this.GroupStyle;
            _adapter.ItemContainerHolderStretchOrientation = Windows.UI.Xaml.Controls.Orientation.Horizontal;
            _adapter.SecondaryPool = _secondaryPool;

            SelectedItems.Safe()
            .ForEach((object i) => _adapter.SetItemSelection(i, null, true));

            Adapter = _adapter;

            IsResetScrollOnItemsSourceChanged = true;
        }