Drag and Drop within the same control

7 posts, 0 answers
  1. Cooltisch
    Cooltisch avatar
    10 posts
    Member since:
    Mar 2007

    Posted 08 Jun 2010 Link to this post

    Hey
    There is an issue if i would like to get the left-top position of the dragcue. In the case i declare a control as a drag and drop container the "TranformToVisual" Method is crashing with an exception "The value is outside of the expected range".

            private void dropBox_DropQuery(object sender, DragDropQueryEventArgs e)
            {
                var topLeft = (e.Options.DragCue as FrameworkElement).TransformToVisual(null).Transform(new Point(0, 0));
                e.QueryResult = true;
                e.Handled = true;
            }

    Best regards Holger
  2. Viktor Tsvetkov
    Admin
    Viktor Tsvetkov avatar
    382 posts

    Posted 08 Jun 2010 Link to this post

    Hello Cooltisch,

    Attached you can find a sample project. Inside a RadWrapPanel I put a rectangle which you can drag and drop on the Canvas next to it. Also I added the line of code which causes you problems. Review the project and feel free to ask me if you have further questions. 

    Greetings,
    Viktor Tsvetkov
    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. Cooltisch
    Cooltisch avatar
    10 posts
    Member since:
    Mar 2007

    Posted 09 Jun 2010 Link to this post

    Thanks for your answer and your example.
    As long as you drag an object from another control/container it works perfect.
    Please change your XAML like that:

    <Canvas x:Name="canvas" Grid.Column="1" Background="Black" Height="500" Width="1000" telerikDragDrop:RadDragAndDropManager.AllowDrop="True">
                <Rectangle Width="100" Height="100" Fill="Red" x:Name="rectangle" telerikDragDrop:RadDragAndDropManager.AllowDrag="True" />
            </Canvas>

    Just  move the rectangle inside of the canvas and your solution does not work any more and this code line
    "var canvasPosition = (e.Options.DragCue as FrameworkElement).TransformToVisual(null).Transform(new Point(0, 0));"
    will throw an exception.


  5. Viktor Tsvetkov
    Admin
    Viktor Tsvetkov avatar
    382 posts

    Posted 09 Jun 2010 Link to this post

    Hello Cooltisch,

    Attached is the edited project. You need to move this problematic line of code in the OnDropInfo method. Please review it and do not hesitate to get back to me if you have further questions.

    Greetings,
    Viktor Tsvetkov
    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.
  6. Cooltisch
    Cooltisch avatar
    10 posts
    Member since:
    Mar 2007

    Posted 10 Jun 2010 Link to this post

    Hey Viktor

    this doesn't work either. I placed the following line inside of the Dropinfo event.

            private void OnDropInfo(object sender, DragDropEventArgs e)
            {
                //This line throws an exception
                var myPosition = (e.Options.DragCue as FrameworkElement).TransformToVisual(null).Transform(new Point(0, 0));

                var destination = e.Options.Destination;

                if (e.Options.Status == DragStatus.DropComplete && destination is Canvas)
                {
                    var payload = e.Options.Payload as FrameworkElement;

                    var canvasPosition = canvas.TransformToVisual(null).Transform(new Point(0, 0));
                    var mousePosition = e.Options.CurrentDragPoint;

                    Canvas.SetLeft(payload, mousePosition.X - canvasPosition.X - payload.ActualWidth / 2);
                    Canvas.SetTop(payload, mousePosition.Y - canvasPosition.Y - payload.ActualHeight / 2);

                    var newChild = e.Options.Payload as Rectangle;
                    canvas.Children.Add(newChild);
                }
            }
    Best regards Holger

  7. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 15 Jun 2010 Link to this post

    Hi Cooltisch,

    Can you please elaborate on your scenario a bit more? That will help us provide you with a better suited solution.

    Also, you can get the position in the OnDragInfo() event handler when the DragStatus is DragInProgress, like so:
    private void OnDragInfo(object sender, DragDropEventArgs e)
    {
        if (e.Options.Status == DragStatus.DragInProgress)
        {
            var myPosition = (e.Options.DragCue as FrameworkElement).TransformToVisual(null).Transform(new Point(0, 0));
        }
                 
        if (e.Options.Status == DragStatus.DragComplete)
        {
            canvas.Children.Remove(e.Source as UIElement);
        }
    }

    I attached a modified project illustrating the suggested approach. Please take a look at it and let me know if it works for you or if your scenario requires a different approach.

    Regards,
    Tina Stancheva
    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.
  8. Matthew
    Matthew avatar
    8 posts
    Member since:
    Oct 2010

    Posted 13 Apr 2011 Link to this post

    This is a great example.  Is it possible to use a Canvas object as the DragCue  instead of a Rectangle?  I have seen examples where Rectangles and Image are used.  However, I would like to use a Canvas.  An example would be great.

    Thanks,
    Matt
Back to Top
DevCraft banner