1

Topic: Button drag and drop on a window - a capture point with a margin error

Good afternoon.
It is necessary to implement drag and drop of the button by a mouse on a window.

private void c_MouseDown (object sender, System. Windows. Input. MouseEventArgs mevent)
{
IsDragMode = true;
}
private void c_MouseUp (object sender, System. Windows. Input. MouseEventArgs mevent)
{
IsDragMode = false;
}
private void c_MouseMove (object sender, System. Windows. Input. MouseEventArgs mevent)
{
if (IsDragMode)
{
System. Windows. Controls. Button c = sender as System. Windows. Controls. Button;
TranslateTransform transform = new TranslateTransform ();
transform. X = Mouse. GetPosition (MainGrid).X;
transform. Y = Mouse. GetPosition (MainGrid).Y;
c. RenderTransform = transform;
}
}

Button creation:

System. Windows. Controls. Button b = new System. Windows. Controls. Button ();
b. Width = 800;
b. Height = 500;
b. Margin = new Thickness (20, 20, 20, 20);
MainGrid. Children. Add (b);
b. PreviewMouseDown + = c_MouseDown;
b. PreviewMouseMove + = c_MouseMove;
b. PreviewMouseUp + = c_MouseUp;

Works, but at pushing of a mouse the button drifts concerning a capture point.
Tried to make correction:

TranslateTransform transform = new TranslateTransform ();
transform. X = Mouse. GetPosition (MainGrid).X;
transform. Y = Mouse. GetPosition (MainGrid).Y;
TranslateTransform transform2 = new TranslateTransform ();
transform2.X = Mouse. GetPosition (c as UIElement).X;
transform2.Y = Mouse. GetPosition (c as UIElement).Y;
transform. X + = transform2.X;
transform. Y + = transform2.Y;
c. RenderTransform = transform;

In this case the button flickers and twitches.

2

Re: Button drag and drop on a window - a capture point with a margin error

Set to the button property SnapsToDevicePixels=True
Or use GuideLines, if contents difficult.

3

Re: Button drag and drop on a window - a capture point with a margin error

Generally on mind it is better to do not so, to make , similar  Thumb in which drag and drop is already implemented, to add it in a template of the button and to use.
As it seems to me.
Usage RenderTransform will not cause breadboard model recalculation, for this purpose is LayoutTransform, it is necessary to consider it at designing .
At implementation of control by means of a mouse connected with  buttons (drag and drop, drawing, selection) and others is better to implement it through capture of a mouse. Capture of a mouse by a control item allows it and only to it to receive coordinates of a mouse, a state of its buttons irrespective of all remaining.
It is naturally necessary to consider, as other control items can capture a mouse, but problems will not be, if they are in 1 tree. Capture of a mouse can be used through the manager of a mouse bool Mouse. Capture (myControl), Mouse. Capture (null) or through a control item bool CaptureMouse (), ReleaseMouseCapture ();
In the manager it is possible as to look at what control item captured a mouse, during debugging it can strongly help during the conflict. And Mouse. Capture has additional parameters.

4

Re: Button drag and drop on a window - a capture point with a margin error

Roman Mejtes wrote:

set to the button property SnapsToDevicePixels=True
Or use GuideLines, if contents difficult.

Changes nothing.
And through Mouse. Captur he will calculate correction of a point of pushing of the mouse concerning boundaries ?

5

Re: Button drag and drop on a window - a capture point with a margin error

In general, dared - it was necessary to consider Margin an element.