Пример #1
0
        private void RectangleSelection_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            itemsPosition.Clear();
            originDragPoint   = new Point(e.GetCurrentPoint(uiElement).Position.X, e.GetCurrentPoint(uiElement).Position.Y); // Initial drag point relative to the topleft corner
            prevSelectedItems = uiElement.SelectedItems.Cast <object>().ToList();                                            // Save current selected items
            var verticalOffset = scrollViewer?.VerticalOffset ?? 0;

            originDragPoint.Y += verticalOffset; // Initial drag point relative to the top of the list (considering scrolled offset)
            if (!e.GetCurrentPoint(uiElement).Properties.IsLeftButtonPressed || e.Pointer.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Touch)
            {
                // Trigger only on left click, do not trigger with touch
                return;
            }

            selectionStrategy = e.KeyModifiers.HasFlag(VirtualKeyModifiers.Control) ?
                                new InvertPreviousItemSelectionStrategy(uiElement.SelectedItems, prevSelectedItems) :
                                e.KeyModifiers.HasFlag(VirtualKeyModifiers.Shift) ?
                                (ItemSelectionStrategy) new ExtendPreviousItemSelectionStrategy(uiElement.SelectedItems, prevSelectedItems) :
                                new IgnorePreviousItemSelectionStrategy(uiElement.SelectedItems);

            selectionStrategy.HandleNoItemSelected();

            uiElement.PointerMoved -= RectangleSelection_PointerMoved;
            uiElement.PointerMoved += RectangleSelection_PointerMoved;
            if (selectionChanged != null)
            {
                // Unsunscribe from SelectionChanged event for performance
                uiElement.SelectionChanged -= selectionChanged;
            }
            uiElement.CapturePointer(e.Pointer);
            selectionState = SelectionState.Starting;
        }
        private void RectangleSelection_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            itemsPosition.Clear();
            dataGridRows.Clear();
            DependencyObjectHelpers.FindChildren <DataGridRow>(dataGridRows, uiElement);                                     // Find visible/loaded rows
            prevSelectedItems = uiElement.SelectedItems.Cast <object>().ToList();                                            // Save current selected items
            originDragPoint   = new Point(e.GetCurrentPoint(uiElement).Position.X, e.GetCurrentPoint(uiElement).Position.Y); // Initial drag point relative to the topleft corner
            var verticalOffset = (scrollBar?.Value ?? 0) - uiElement.ColumnHeaderHeight;

            originDragPoint.Y += verticalOffset; // Initial drag point relative to the top of the list (considering scrolled offset)
            if (!e.GetCurrentPoint(uiElement).Properties.IsLeftButtonPressed || e.Pointer.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Touch)
            {
                // Trigger only on left click, do not trigger with touch
                return;
            }

            var clickedRow = DependencyObjectHelpers.FindParent <DataGridRow>(e.OriginalSource as DependencyObject);

            if (clickedRow != null && uiElement.SelectedItems.Contains(clickedRow.DataContext))
            {
                // If the item under the pointer is selected do not trigger selection rectangle
                return;
            }

            var selectedItems = new GenericItemsCollection <object>(uiElement.SelectedItems);

            selectionStrategy = e.KeyModifiers.HasFlag(VirtualKeyModifiers.Control) ?
                                new InvertPreviousItemSelectionStrategy(selectedItems, prevSelectedItems) :
                                e.KeyModifiers.HasFlag(VirtualKeyModifiers.Shift) ?
                                (ItemSelectionStrategy) new ExtendPreviousItemSelectionStrategy(selectedItems, prevSelectedItems) :
                                new IgnorePreviousItemSelectionStrategy(selectedItems);

            if (clickedRow == null)
            {
                // If user click outside, reset selection
                if (uiElement.CurrentColumn != null)
                {
                    uiElement.CancelEdit();
                }
                DeselectGridCell();
                selectionStrategy.HandleNoItemSelected();
            }

            uiElement.PointerMoved -= RectangleSelection_PointerMoved;
            uiElement.PointerMoved += RectangleSelection_PointerMoved;
            if (selectionChanged != null)
            {
                // Unsunscribe from SelectionChanged event for performance
                uiElement.SelectionChanged -= selectionChanged;
            }
            uiElement.CapturePointer(e.Pointer);
            selectionState = SelectionState.Starting;
        }