private static void Control_PreviewMouseDown(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 - PARENT and index of the treeViewItem the user clicked on for later. if (SelectedTreeViewItem != null) { ptMouseDown = MouseUtilities.GetMousePosition(ThisTreeView); //indexToSelect = index; //MessageBox.Show(ptMouseDown.X.ToString() + " " + ptMouseDown.Y.ToString()); //e.Handled = true; } else { ptMouseDown = new Point(-10000, -10000); indexToSelect = null;// -1; // e.Handled = true; } }
static 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)); }
static void UpdateDragAdornerLocation() { if (dragAdorner != null) { Point ptCursor = MouseUtilities.GetMousePosition(ThisTreeView); double left = ptCursor.X - ptMouseDown.X; // 4/13/2007 - Made the top offset relative to the item being dragged. TreeViewItem itemBeingDragged = GettreeViewItem(indexToSelect); Point itemLoc = itemBeingDragged.TranslatePoint(new Point(0, 0), ThisTreeView); double top = itemLoc.Y + ptCursor.Y - ptMouseDown.Y; dragAdorner.SetOffsets(left, top); } }
static AdornerLayer InitializeAdornerLayer(TreeViewItem itemToDrag) { // Create a brush which will paint the treeViewItem onto // a visual in the adorner layer. VisualBrush brush = new VisualBrush(SelectedTreeViewItem); // Create an element which displays the source item while it is dragged. dragAdorner = new DragAdorner(SelectedTreeViewItem, SelectedTreeViewItem.RenderSize, brush) { // Set the drag adorner's opacity. Opacity = 0.7// DragAdornerOpacity }; AdornerLayer layer = AdornerLayer.GetAdornerLayer(ThisTreeView); layer.Add(dragAdorner); // Save the location of the cursor when the left mouse button was pressed. ptMouseDown = MouseUtilities.GetMousePosition(ThisTreeView); return(layer); }