/// <summary> /// Handles the mouse left button down click event /// </summary> /// <param name="e"></param> protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e) { base.OnPreviewMouseLeftButtonDown(e); if (!e.Source.Equals(this)) // do drag if the source is the DragCanvas { isDown = true; // mouse is down, save the click point startpoint = e.GetPosition(this); // if an element clicked or a child of the element is clicked and has the IsDraggable property the proceed VisualTreeHelper.HitTest((UIElement)e.Source, new HitTestFilterCallback(MyHitTestFilter), new HitTestResultCallback(MyHitTestResult), new PointHitTestParameters(e.GetPosition((UIElement)e.Source))); if (isdrag) { // if the element is a MwiChild then stop, otherwise find if the element has a parent type of MwiChild if (e.Source.GetType().Equals(typeof(AnalysisChild))) { originalElement = (UIElement)e.Source; } else { originalElement = (UIElement)VisualTreeSearch.FindByParentType((UIElement)e.Source, typeof(AnalysisChild)); } if (originalElement != null) { int zindex = Canvas.GetZIndex(originalElement); // set the s-index of the element ((AnalysisChild)originalElement).IsSelected = true; // select the element because is was clicked this.CaptureMouse(); // capture the mouse e.Handled = true; // handle the event } } } }
/// <summary> /// Drag the element about the Canvas by updating its Canavas position. /// </summary> /// <param name="currentPosition"></param> private void DragMoved(Point currentPosition) { double elementLeft = (currentPosition.X - startpoint.X) + originalLeft; double elementTop = (currentPosition.Y - startpoint.Y) + originalTop; // Find the panel under the drag canavas and get its size. This way items can be dragged over // the minimized child elements DockPanel panel = (DockPanel)VisualTreeSearch.FindByParentType(this, typeof(DockPanel)); Size panelSize = this.RenderSize; if (panel != null) { panelSize = panel.RenderSize; } // make sure the element is within the bounds of the DragCanvas if (elementLeft < 0) { elementLeft = 0; } else if ((elementLeft + originalElement.RenderSize.Width) > panelSize.Width) { elementLeft = panelSize.Width - originalElement.RenderSize.Width; } Canvas.SetLeft(originalElement, elementLeft); // make sure the element is within the bounds of the DragCanvas if (elementTop < 0) { elementTop = 0; } else if ((elementTop + originalElement.RenderSize.Height) > panelSize.Height) { elementTop = panelSize.Height - originalElement.RenderSize.Height; } Canvas.SetTop(originalElement, elementTop); }