I am trying to start a drag and drop on a Button control in WPF. I am using Button because I also want to handle click event.
Adding a Button in XAML and handling the MouseMove event always has e.LeftMouse equal to MouseButtonState.Released.
<Button MouseMove="Button_MouseMove"/>
In the following handler implementation the exception is never thrown.
private void Button_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
throw new Exception("It works!");
}
I noticed the same is true for any control I place inside a Button and try to process MouseMove event.
How do I handle drag and drop from a Button control or any other control inside a button in WPF?
EDIT - Solution based on mm8's answer
The updated XAML is:
<Button PreviewMouseDown="Button_PreviewMouseDown"
PreviewMouseUp="Button_PreviewMouseUp"
PreviewMouseMove="Button_PreviewMouseMove">
</Button>
The updated handler code:
Point startPosition;
double delta = 10;
private void Button_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
startPosition = e.GetPosition(this);
}
private void Button_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
Point currentPosition = e.GetPosition(this);
if ((currentPosition - startPosition).Length < delta)
throw new Exception("MouseClick");
}
private void Button_PreviewMouseMove(object sender, MouseEventArgs e)
{
Point currentPosition = e.GetPosition(this);
double currentDelta = (currentPosition - startPosition).Length;
if (e.LeftButton == MouseButtonState.Pressed && currentDelta >= delta)
throw new Exception("DragAndDrop");
}
You could perhaps handle the PreviewMouseDown event instead of Click:
private void Button_Click(object sender, MouseButtonEventArgs e)
{
//handle click here...
e.Handled = true;
}
XAML:
<Button PreviewMouseDown="Button_Click" MouseMove="Button_MouseMove"/>
Related
I have a borderless, transparent Window with only one Button.
My fancy button looks like this
The expected behaviour is:
When I click and drag the Button, the Button must follow the cursor.
When I only click on the Button, the MouseDown, PreviewMouseDown events or Command binding should raise.
At first I tried to call the DragMove() on PreviewMouseDown event, but that blocks the click events. Now my idea is: I set a 100ms delay after mouse down. If the time passed, than the button wil be dragging, otherwise it was just a click.
Code
private bool _dragging;
private Point startpos;
CancellationTokenSource cancellation;
private void Button_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (_dragging && e.LeftButton == MouseButtonState.Pressed)
{
var currentpos = e.GetPosition(this);
Left += currentpos.X - startpos.X;
Top += currentpos.Y - startpos.Y;
}
}
private async void Button_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton != MouseButton.Left)
return;
_dragging = false;
startpos = e.GetPosition(this);
cancellation?.Cancel();
cancellation = new CancellationTokenSource();
await Task.Delay(100, cancellation.Token).ContinueWith(task =>
{
_dragging = !task.IsCanceled;
});
}
private void Button_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
if (_dragging)
{
_dragging = false;
e.Handled = true;
}
cancellation?.Cancel();
}
Basically it works, but has some bugs:
When I hold down the mouse button for longer time and release, then the click won't work, because after 100ms the dragging will be active.
After I dragged the button, and click anywhere outside the Button and Window control, the PreviewMouseDown and PreviewMouseUp events are raised. I don't know why??
Does somebody have any better solution?
For your first problem:
You must handle the case when your button is pushed down but not moved. I think a better way to do this (instead of the 100ms delay) would be to specify a minimum threshold of movement above which the dragging will start.
You could do it like this:
private const double _dragThreshold = 1.0;
private bool _dragging;
private Point startpos;
CancellationTokenSource cancellation;
private void Button_PreviewMouseMove(object sender, MouseEventArgs e)
{
var currentpos = e.GetPosition(this);
var delta = currentpos - startpos;
if ((delta.Length > _dragThreshold || _dragging) && e.LeftButton == MouseButtonState.Pressed)
{
_dragging = true;
Left += currentpos.X - startpos.X;
Top += currentpos.Y - startpos.Y;
}
}
private async void Button_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton != MouseButton.Left)
return;
_dragging = false;
startpos = e.GetPosition(this);
cancellation?.Cancel();
cancellation = new CancellationTokenSource();
}
For your second problem: the button will capture the mouse on the mouse down event.
You need to release the captured mouse with the ReleaseMouseCapture method when you are done with your drag.
private void Button_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
if (_dragging)
{
_dragging = false;
e.Handled = true;
var button = sender as Button;
button.ReleaseMouseCapture();
}
cancellation?.Cancel();
}
Here's my code:
public AbilitiesController(Abilities page)
{
_page = page;
page.ScrollBar.MouseLeftButtonDown += MouseDown;
page.ScrollBar.MouseLeftButtonUp += MouseUp;
page.ScrollBar.MouseLeave += MouseLeave;
page.ScrollBar.MouseWheel += MouseWheel;
page.ScrollBar.MouseMove += MouseMove;
}
private void MouseMove(object sender, MouseEventArgs mouseEventArgs)
{
if (!_dragBound) return;
var newPos = mouseEventArgs.GetPosition(_page);
var dPos = newPos - _pos;
_page.ScrollBar.ScrollToHorizontalOffset(dPos.X);
_page.ScrollBar.ScrollToVerticalOffset(dPos.Y);
_pos = newPos;
Console.WriteLine("Moved");
}
private void MouseWheel(object sender, MouseWheelEventArgs mouseWheelEventArgs)
{
Console.WriteLine("MouseWheel");
}
private void MouseLeave(object sender, MouseEventArgs mouseEventArgs)
{
_dragBound = false;
Console.WriteLine("Left");
}
private void MouseUp(object sender, MouseButtonEventArgs mouseButtonEventArgs)
{
_dragBound = false;
Console.WriteLine("Mouse Up");
}
private void MouseDown(object sender, MouseButtonEventArgs mouseButtonEventArgs)
{
_dragBound = true;
Console.WriteLine("Click!");
}
Page is a Page, Scrollbar is a Scrollview.
I know my logic for moving the scrollbar probably isn't correct yet, but we aren't even getting that far yet.
For some odd reason the MouseDown event isn't firing whether I bind it to MouseDown or MouseLeftButtonDown.
However oddly enough the MouseUp event works no problem.
Which seems really odd because normally if one is broken, the other is too...
Using latest version of Visual Studio 2017 in a WPF project.
You might want to try 'PreviewMouseDown' instead of 'MouseDown' and same for all the other non-working mouse events, as they may already be being handled by whatever the base class of your 'Abilities' class is.
I have this code for dragging my Window with its MouseDown Event.
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
this.DragMove();
}
But I want to do this using a Button, because my form is transparent. And Using the same function for that button's MouseDown event will not work.
How can I achieve this?
You have to use PreviewMouseDown instead of MouseDown event.
And do not forget to mark the event "Handled" in the end.
XAML code:
<Button x:Name="Button_Move" PreviewMouseDown="Window_Main_MouseDown"/>
C# code:
private void Window_Main_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == System.Windows.Input.MouseButton.Left)
{
this.DragMove();
e.Handled = true;
}
}
Use border instead of Button. Because DragMove only work on PrimaryMouseButton event. Not work on Click event
XAML
<Border Background="Blue" MouseLeftButtonDown="Border_MouseLeftButtonDown">
</Border>
CODE
private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
My Finally Code was here:
private Point startPoint;
private void btnChat_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
startPoint = e.GetPosition(btnChat);
}
private void btnChat_PreviewMouseMove(object sender, MouseEventArgs e)
{
var currentPoint = e.GetPosition(btnChat);
if (e.LeftButton == MouseButtonState.Pressed &&
btnChat.IsMouseCaptured &&
(Math.Abs(currentPoint.X - startPoint.X) >
SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(currentPoint.Y - startPoint.Y) >
SystemParameters.MinimumVerticalDragDistance))
{
// Prevent Click from firing
btnChat.ReleaseMouseCapture();
DragMove();
}
}
I have Form with textboxes and they have MouseEvent(MouseMove, MouseDown) and they are enabled on form load, but my question is how to call them just when i Click the Edit Button, so just then the textboxes can move?
My code:
private void textBox_MouseMove(object sender, MouseEventArgs e)
{
TextBox txt = sender as TextBox;
foreach (TextBox text in textBoxs)
{
if (e.Button == MouseButtons.Left)
{
if (txt.Name == text.Name)
{
txt.Left = e.X + txt.Left - MouseDownLocation.X;
txt.Top = e.Y + txt.Top - MouseDownLocation.Y;
}
}
}
}
private void textBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
}
private void btnEdit_Click(object sender, EventArgs e)
{
btnEdit.Visible = false;
btnPrint.Visible = false;
btnSave.Visible = true;
//Want to call mouse function here.
}
any suggestions?
Instead of hooking the events from the Visual Studio designer, you should manually hook the events in the btnEdit_Click handler method by adding this line:
textboxname.MouseMove += new MouseEventHandler(textBox_MouseMove);
Then unhook the event when your save button is clicked (I'm assuming you have some method btnSave_Click) by doing the following:
textboxname.MouseMove -= new MouseEventHandler(textBox_MouseMove);
The same goes for your MouseDown event.
If I understand your post, you want the textbox functionality to become "active" after you click the btnEdit button?
You could set a flag in btnEdit_Click, and only process the functionality in the other functions if that flag is true
Or, maybe add the event in the btnEdit_Click function, e.g.
private void btnEdit_Click(object sender, EventArgs e)
{
btnEdit.Visible = false;
btnPrint.Visible = false;
btnSave.Visible = true;
//Want to call mouse function here.
textBox.MouseDown += new MouseEventHandler(textBox_MouseDown);
}
But remove that extra line from where it currently exists in your code.
I am trying to make my Form, which has no border, moveable with holding the left mousebutton down and exit the while loop, when releasing the mousebutton.
But the code I have right now doesn't exit the loop on release.
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
while (mouseDown)
{
mouseX = MousePosition.X;
mouseY = MousePosition.Y - 30;
this.SetDesktopLocation(mouseX, mouseY);
if (e.Button != MouseButtons.Left)
mouseDown = false;
}
I also tried to add a mouseUp event but it cant happen as long as mouseDown is active.
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = true;
}
OK, I fixed it for myself.
I just did this:
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDown)
{
mouseX = MousePosition.X - 20;
mouseY = MousePosition.Y - 40;
this.SetDesktopLocation(mouseX, mouseY);
}
}
By using a loop in the on mouse up event you are locking the thread. You could use the MouseMove event with a public variable to check if the mouse is down.