void listView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (IsMouseOverScrollbar) { // 4/13/2007 - Set the flag to false when cursor is over scrollbar. canInitiateDrag = false; return; } int index = IndexUnderDragCursor; canInitiateDrag = index > -1; if (canInitiateDrag) { // Remember the location and index of the ListViewItem the user clicked on for later. ptMouseDown = MouseUtilities.GetMousePosition(listView); indexToSelect = index; } else { ptMouseDown = new Point(-10000, -10000); indexToSelect = -1; } }
bool IsMouseOver(Visual target) { // We need to use MouseUtilities to figure out the cursor // coordinates because, during a drag-drop operation, the WPF // mechanisms for getting the coordinates behave strangely. Rect bounds = VisualTreeHelper.GetDescendantBounds(target); Point mousePos = MouseUtilities.GetMousePosition(target); return(bounds.Contains(mousePos)); }
void UpdateDragAdornerLocation() { if (dragAdorner != null) { Point ptCursor = MouseUtilities.GetMousePosition(ListView); double left = ptCursor.X - ptMouseDown.X; // 4/13/2007 - Made the top offset relative to the item being dragged. ListViewItem itemBeingDragged = GetListViewItem(indexToSelect); Point itemLoc = itemBeingDragged.TranslatePoint(new Point(0, 0), ListView); double top = itemLoc.Y + ptCursor.Y - ptMouseDown.Y; dragAdorner.SetOffsets(left, top); } }
AdornerLayer InitializeAdornerLayer(ListViewItem itemToDrag) { // Create a brush which will paint the ListViewItem onto // a visual in the adorner layer. VisualBrush brush = new VisualBrush(itemToDrag); // Create an element which displays the source item while it is dragged. dragAdorner = new DragAdorner(listView, itemToDrag.RenderSize, brush); // Set the drag adorner's opacity. dragAdorner.Opacity = DragAdornerOpacity; AdornerLayer layer = AdornerLayer.GetAdornerLayer(listView); layer.Add(dragAdorner); // Save the location of the cursor when the left mouse button was pressed. ptMouseDown = MouseUtilities.GetMousePosition(listView); return(layer); }