1
votes

I have created a thumb in WPF. I use the DragDelta event to change a value using a mouse click and drag.

Here is my DragDelta code:

private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
    MyValue = e.VerticalChange;
}

This works fine, however, when clicking my button again, the value starts at the point where I clicked (0). I need the click and drag to change the value relative to the original. So I tried this:

private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
    MyValue += e.VerticalChange;
}

This works but when I drag down and then up again, the value keeps decreasing, even though I move the mouse back up. Same thing when moving the mouse up (value increases), and then move the mouse back down (value keeps increasing).

1
I ran into this issue myself. It turns out that under certain circumstances, e.HorizontalChange and e.VerticalChange are relative to the last DragStart event, not the last DragDelta event.rookie1024
I also run into that issue. The problem disappeared as soon as I started the application in release mode instead of debug mode.chriga

1 Answers

0
votes

This is how it works:

double minLeft = double.MaxValue;
double minTop = double.MaxValue;
double left = Canvas.GetLeft(item);
double top = Canvas.GetTop(item);

minLeft = double.IsNaN(left) ? 0 : Math.Min(left, minLeft);
minTop = double.IsNaN(top) ? 0 : Math.Min(top, minTop);

double deltaHorizontal = Math.Max(-minLeft, e.HorizontalChange);
double deltaVertical = Math.Max(-minTop, e.VerticalChange);

replace your container control with the canvas.