Dropping into Viewbox

4 posts, 1 answers
  1. Tom
    Tom avatar
    6 posts
    Member since:
    Jun 2010

    Posted 21 Oct 2010 Link to this post

    I have my drop and drag working to drop object into Canvas wrapped in a ViewBox.    The viewbox is scaling the canvas so that it displays the fill object on the screen.

    My problem is that the coordinates within the canvas are no longer the same as the screen coordinates.  I'm currently using below code from examples -

    Camera1Canvas.Children.Add(userControl);
    // Determine the drop point for the new UserControl.
    Point canvasPosition = Camera1Canvas.TransformToVisual(null).Transform(new Point(0, 0));
    var userControlDesiredPosition = new Point
               {
                  X = e.Options.CurrentDragPoint.X - canvasPosition.X,
                  Y = e.Options.CurrentDragPoint.Y - canvasPosition.Y
                };
    // e.Options.CurrentDragPoint returns the absolute mouse position,
    // that is why we have to transform the host canvas and subtract its position from the absolute mouse position.
    userControl.SetValue(Canvas.LeftProperty, canvasPosition1.X);
    userControl.SetValue(Canvas.TopProperty, canvasPosition1.Y);
    userControl.SetValue(Canvas.ZIndexProperty, Camera1Canvas.Children.Count);
    userControl.TabIndex = Camera1Canvas.Children.Count;

    And this code works just fine if I'm positioning object to the screen coordinates.  I need to understand how to map those coordinates so that object is placed in the correct location inside the canvas inside of the viewbox.

    Tom
  2. Tsvyatko
    Admin
    Tsvyatko avatar
    832 posts

    Posted 21 Oct 2010 Link to this post

    Hi Tom,

    Please, have a look at this thread - http://www.telerik.com/community/forums/silverlight/drag-and-drop/drag-and-drop-from-listbox-to-canvas-that-has-a-transform.aspx. Let me know if this solves the problem or if you have any futher questions.

    Regards,
    Tsvyatko
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft banner
  4. Tom
    Tom avatar
    6 posts
    Member since:
    Jun 2010

    Posted 21 Oct 2010 Link to this post

    I could not get the code to work as written.  In GetPropPoint() routine, scaleTransform was NULL.  I added the following line

    if (scaleTransform == null
       scaleTransform = new ScaleTransform {ScaleX = transform.Matrix.M11, ScaleY = 
                             transform.Matrix.M22};

    It is now dropping correctly, except that it is putting the upper right corner of the object as the mouse position.  Pardon my lack of xaml knowledge, but how do I determine where the upper left corner of my object is realitive to the e.Options.CurrentDragPoint?

  5. Answer
    Tsvyatko
    Admin
    Tsvyatko avatar
    832 posts

    Posted 22 Oct 2010 Link to this post

    Hi Tom,

    I guess that what you are trying to achieve is when the item is dropped in the canvas its position corresponds to the mouse position relative to the dragged element. I have modified the sample project to illustrate my idea. Please, have a look and let me know if this works in our scenario.

    Best wishes,
    Tsvyatko
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top