Пример #1
0
        private static void DragMultiSelectEnabled_ListView_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var   listView = sender as ListView;
            Point currentPointOnListView = e.GetPosition(listView);
            ListViewDragSelectAdorner dragSelectAdorner = GetListViewDragSelectAdorner(listView);

            // select the item when adorner is not displayed.
            if (!IsCtrlShiftDown() && dragSelectAdorner == null)
            {
                HitTestResult hitTestResult = VisualTreeHelper.HitTest(listView, currentPointOnListView);
                var           hitItem       = hitTestResult == null ? null : GetAncestorListViewItem(hitTestResult.VisualHit);
                if (hitItem != null)
                {
                    listView.UnselectAll();
                    hitItem.Focus();
                    hitItem.IsSelected = true;
                }
            }

            SetDragMultiSelectStartPoint(listView, null);
            if (dragSelectAdorner != null)
            {
                RemoveListViewDragSelectAdorner(listView);
                listView.ReleaseMouseCapture();

                // prevent deselecting by default listview behavior
                if (!IsCtrlShiftDown())
                {
                    e.Handled = true;
                }
            }
        }
Пример #2
0
        private static void RemoveListViewDragSelectAdorner(ListView listView)
        {
            var          target       = GetListViewItemsPanel(listView);
            AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(target);
            ListViewDragSelectAdorner dragSelectAdorner = GetListViewDragSelectAdorner(listView);

            adornerLayer.Remove(dragSelectAdorner);
            SetListViewDragSelectAdorner(listView, null);
        }
Пример #3
0
        private static void AddListViewDragSelectAdorner(ListView listView, Point startPoint)
        {
            var          target       = GetListViewItemsPanel(listView);
            AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(target);
            ListViewDragSelectAdorner dragSelectAdorner = new ListViewDragSelectAdorner(target)
            {
                StartPoint = startPoint, EndPoint = startPoint
            };

            adornerLayer.Add(dragSelectAdorner);
            SetListViewDragSelectAdorner(listView, dragSelectAdorner);
        }
Пример #4
0
        private static void DragMultiSelectEnabled_ListView_Scrolled(object sender, ScrollChangedEventArgs e)
        {
            var listView = sender as ListView;
            ListViewDragSelectAdorner dragSelectAdorner = GetListViewDragSelectAdorner(listView);

            if (dragSelectAdorner != null)
            {
                dragSelectAdorner.StartPoint = new Point(
                    dragSelectAdorner.StartPoint.X - e.HorizontalChange,
                    dragSelectAdorner.StartPoint.Y - e.VerticalChange);
            }
        }
Пример #5
0
        private static void DragMultiSelectEnabled_ListView_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            var listView   = sender as ListView;
            var itemsPanel = GetListViewItemsPanel(listView);
            ListViewDragSelectAdorner dragSelectAdorner = GetListViewDragSelectAdorner(listView);
            Point currentPointOnItemsPanel = e.GetPosition(itemsPanel);
            Point?dragStartPointNull       = GetDragMultiSelectStartPoint(listView);

            if (e.MouseDevice.LeftButton == MouseButtonState.Pressed)
            {
                // start drag adorner
                if (dragSelectAdorner == null && dragStartPointNull.HasValue)
                {
                    Point dragStartPoint = dragStartPointNull.Value;
                    if (Math.Abs(dragStartPoint.X - currentPointOnItemsPanel.X) > SystemParameters.MinimumHorizontalDragDistance ||
                        Math.Abs(dragStartPoint.Y - currentPointOnItemsPanel.Y) > SystemParameters.MinimumVerticalDragDistance)
                    {
                        AddListViewDragSelectAdorner(listView, dragStartPoint);
                        listView.CaptureMouse();
                        if (!IsCtrlShiftDown())
                        {
                            listView.UnselectAll();
                        }
                    }
                    else
                    {
                        // nothing to do
                    }
                }

                dragSelectAdorner = GetListViewDragSelectAdorner(listView);
                if (dragSelectAdorner != null)
                {
                    // Expand the adorner
                    dragSelectAdorner.EndPoint = currentPointOnItemsPanel;

                    // select the items being under the adorner
                    var items = itemsPanel.Children;
                    foreach (ListViewItem item in items)
                    {
                        item.IsSelected = IsItemUnderAdorner(itemsPanel, item, dragSelectAdorner);
                    }
                }
            }
        }
Пример #6
0
        private static bool IsItemUnderAdorner(UIElement parent, FrameworkElement item, ListViewDragSelectAdorner adorner)
        {
            Point  iTopLeft     = item.TranslatePoint(new Point(0, 0), parent);
            Point  iRightButtom = item.TranslatePoint(new Point(item.ActualWidth, item.ActualHeight), parent);
            double aLeft        = Math.Min(adorner.StartPoint.X, adorner.EndPoint.X);
            double aRight       = Math.Max(adorner.StartPoint.X, adorner.EndPoint.X);
            double aTop         = Math.Min(adorner.StartPoint.Y, adorner.EndPoint.Y);
            double aButtom      = Math.Max(adorner.StartPoint.Y, adorner.EndPoint.Y);

            return(IsRangeOverlap(iTopLeft.X, iRightButtom.X, aLeft, aRight) &&
                   IsRangeOverlap(iTopLeft.Y, iRightButtom.Y, aTop, aButtom));
        }
Пример #7
0
 private static void SetListViewDragSelectAdorner(DependencyObject obj, ListViewDragSelectAdorner value)
 {
     obj.SetValue(ListViewDragSelectAdornerProperty, value);
 }