protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e) { base.OnPreviewMouseLeftButtonDown(e); // call the handler of the base first m_IsDragInProgress = false; m_InitialCursorLocation = e.GetPosition(this); // Cache the mouse cursor location //check if the mouse was clicked on the ScrollBar, in which case let the ScrollBar remain its control. if (null != GetParentOfType <ScrollBar>(e.OriginalSource as DependencyObject)) { return; } // Walk up the visual tree from the element that was clicked, // looking for an element that is a direct child of the Canvas DraggedElement = FindCanvasChild(e.Source as DependencyObject); if (m_DraggedElement == null) { return; } // Custom - select dragged element if (DraggedElement.GetType() == typeof(Rectangle) || DraggedElement.GetType() == typeof(Ellipse)) { Shape element = (Shape)DraggedElement; MainWindow window = GetMainWindow(); window.SelectItem(element); } m_ModificationDirection = ResolveOffset(Canvas.GetLeft(m_DraggedElement), Canvas.GetRight(m_DraggedElement), Canvas.GetTop(m_DraggedElement), Canvas.GetBottom(m_DraggedElement)); // Set the Handled flag so that a control being dragged // does not react to the mouse input e.Handled = true; if (m_ModificationDirection == ModificationDirection.None) { DraggedElement = null; return; } BringToFront(m_DraggedElement as UIElement); m_IsDragInProgress = true; }