How to move a control on the window?

0 like 0 dislike
There is a window in this TextBlock. I need to drag this TextBlock around the window. Been looking on Google, found it is done using PreviewMouseLeftButtonDown, PreviewMouseLeftButtonUp , and MouseMove. But what I couldn't figure out how to do it.
Please tell me.
by | 7 views

1 Answer

0 like 0 dislike
Declare a variable to allow drag and drop
private Boolean can = false;

If the user holds the button, the variable to true.
Pressed to false.
private void lbl_MouseLeftButtonUp(Object sender, MouseButtonEventArgs e) { can = false; } private void lbl_MouseLeftButtonDown(Object sender, MouseButtonEventArgs e) { can = true; }

private void Window_MouseMove(Object sender, MouseEventArgs e) { // cancel the drag if the user pressed the button. if (!can) return; Control control = this.lbl; //Get the position of the mouse. Double x = e.GetPosition(this).X y = e.GetPosition(this).Y; //Set the value through margin control.Margin = new Thickness(x, y, 0, 0); }

Related questions

0 like 0 dislike
1 answer
asked May 20, 2019 by Amffore
0 like 0 dislike
2 answers
110,608 questions
257,186 answers
33,650 users