Mouse click and drag Event WPF Mouse click and drag Event WPF wpf wpf

Mouse click and drag Event WPF


I think this is the easiest and most straightforward way :

 private void Window_MouseMove(object sender, MouseEventArgs e) {     if (e.LeftButton == MouseButtonState.Pressed) {        this.DragMove();     } }


public bool dragAction = false;private void minuteHand_MouseLeftButtonDown(object sender, MouseButtonEventArgs e){    dragAction = true;    minuteHand_MouseMove(this.minuteHand, e);}private void minuteHand_MouseMove(object sender, MouseEventArgs e){    if (dragAction == true)    {       this.DragMove();    } } private void minuteHand_MouseLeftButtonUp(object sender, MouseEventArgs e) {    dragAction = false; }

does the trick


You can make things easier and need not handle mouse down / up :

private void minuteHand_MouseMove(object sender, MouseEventArgs e){    if (Mouse.LeftButton == MouseButtonState.Pressed)    {        //my code: moving the needle    } }