This is a migrated thread and some comments may be shown as answers.

Drag and Drop within the same control

6 Answers 99 Views
DragAndDrop
This is a migrated thread and some comments may be shown as answers.
Holger
Top achievements
Rank 1
Holger asked on 08 Jun 2010, 09:09 AM
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

6 Answers, 1 is accepted

Sort by
0
Viktor Tsvetkov
Telerik team
answered on 08 Jun 2010, 03:46 PM
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.
0
Holger
Top achievements
Rank 1
answered on 09 Jun 2010, 06:25 AM
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.


0
Viktor Tsvetkov
Telerik team
answered on 09 Jun 2010, 10:11 AM
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.
0
Holger
Top achievements
Rank 1
answered on 10 Jun 2010, 11:47 AM
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

0
Tina Stancheva
Telerik team
answered on 15 Jun 2010, 02:45 PM
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.
0
Matthew
Top achievements
Rank 1
answered on 13 Apr 2011, 05:42 AM
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
Tags
DragAndDrop
Asked by
Holger
Top achievements
Rank 1
Answers by
Viktor Tsvetkov
Telerik team
Holger
Top achievements
Rank 1
Tina Stancheva
Telerik team
Matthew
Top achievements
Rank 1
Share this question
or