Hi all,
I just had a question regarding constraining a control's drag and drop movement to its parent canvas. I tried using the ConstrainToParentBounds property on the MouseDragElementBehavior, however, when this is used the drag must be done really slowly or the movement of the control is choppy or stops altogether.
So I am attempting to implement my own boundary constraints. I seem to be running into difficulty though. I am still using the MouseDragElementBehavior but am attempting to supplement it by also handling mouseleftbuttondown, mousemove, mouseleftbuttonup events. I know that these are working (haven't been overridden by the MouseDragElementBehavior) as I have tested them using other methods. I will post my current code below:
private void Control_MouseMove(object sender, MouseEventArgs e)
{
MyControl mc = (MyControl)sender;
Canvas canvas = (Canvas)mc.parent;
GeneralTransform ct = canvas.TransformToVisual(Application.Current.RootVisual as UIElement;
Point canvas_offset = ct.Transform(new Point(0,0));
double canvasTop = canvas_offset.Y;
double canvasLeft = canvas_offset.X;
GeneralTransform gt = mc.TransformToVisual(Application.Current.RootVisual as UIElement);
Point offset = gt.Transform(new Point(0,0));
double controlTop = offset.Y;
double controlLeft = offset.X;
if(isMouseCaptured)
{
if(controlTop < canvasTop)
{
mc.Opacity = 1; //to test if conditions are being met, seems to indicate ok
mc.setValue(Canvas.TopProperty, canvasTop);
}
if(controlLeft < canvasLeft)
{
mc.Opacity = 1;
mc.setValue(Canvas.TopProperty, canvasTop);
}
}
}
This is what my code looks like at the moment (I realize there is nothing there for right/bottom). I've tried a bunch of different things at this point and none of them seem to give the desired result; the control's movement is still not constrained to the canvas. Any help/pointers would be greatly appreciated.
Thanks!