Polygon dragging.

2 posts, 0 answers
  1. Fred
    Fred avatar
    11 posts
    Member since:
    Mar 2009

    Posted 06 Mar 2009 Link to this post

    Hi First post here..

    I have started playing around with the drag and drop functionnality.

    So far I am able to drag around on a canvas dynamically drawn Rectangle.

    Problem is when i try to do the same on Polygons (triangle)

    When I click to pick up the polygon, it just to the right and botton a bit... out of the mouse click range.. all tries have same behavior until polygon has moved itself out of the canvas (right - bottom)

    The code i use is there.

    Partial Public Class DragDrop
        Inherits UserControl
        Private myPageSwitch As PageSwitch

        Public Sub New(ByVal ps As PageSwitch)

            myPageSwitch = ps

            Me.dropCanvas.[AddHandler](RadDragAndDropManager.DragQueryEvent, New EventHandler(Of DragDropQueryEventArgs)(AddressOf OnDragQuery))
            Me.dropCanvas.[AddHandler](RadDragAndDropManager.DropQueryEvent, New EventHandler(Of DragDropQueryEventArgs)(AddressOf OnDropQuery))


            'For i As Integer = 1 To 4
            '    CreateARectangle(50, 50, 0, 0, Colors.Red)

        End Sub

        Private Sub OnDragQuery(ByVal sender As Object, ByVal e As DragDropQueryEventArgs)
            If e.Options.Status = DragStatus.DragQuery Then
                e.QueryResult = True
                e.Handled = True
            End If

            If e.Options.Status = DragStatus.DropSourceQuery Then
                e.QueryResult = True
                e.Handled = True
            End If
        End Sub

        Private Sub OnDropQuery(ByVal sender As Object, ByVal e As DragDropQueryEventArgs)
            If e.Options.Status = DragStatus.DropDestinationQuery Then
                e.QueryResult = True

                'Get the image:
                Dim image = TryCast(e.Options.Source, FrameworkElement)

                'Get where the canvas is located relative to the root:
                Dim canvasPosition = dropCanvas.TransformToVisual(Nothing).Transform(New Point(0, 0))
                Dim mousePosition = e.Options.CurrentDragPoint

                'Move the image accordingly:
                Canvas.SetLeft(image, mousePosition.X - canvasPosition.X - image.ActualWidth / 2)
                Canvas.SetTop(image, mousePosition.Y - canvasPosition.Y - image.ActualHeight / 2)
            End If
        End Sub

        Sub BuildTriangle()
            Dim pol As New Polygon
            With pol
                .Stroke = New SolidColorBrush(Colors.Black)
                .Fill = New SolidColorBrush(Colors.Green)
                .Points.Add(New Point(20, 60))
                .Points.Add(New Point(60, 80))
                .Points.Add(New Point(80, 20))
            End With

            RadDragAndDropManager.SetAllowDrag(pol, True)
            RadDragAndDropManager.SetAllowDrop(pol, True)


        End Sub

    Thanks in advance

  2. Miroslav
    Miroslav avatar
    922 posts

    Posted 13 Mar 2009 Link to this post

    Hi Fred,

    First, sorry for the delayed answer. We have been busy with the Q1 release. Your feedback is welcome!

    Now, the DragDrop:

    It is best not to set drag/drop properties in the constructor of the root visual element since the DragDrop needs the root visual element for initialization.

    Also, the drag does not go very smoothly, because the path is set as a drop target, while it is best to set the canvas as a drop target.

    It seems that the path will always return it's actual size as (0,0) which is not very helpful. This is why the path will always appear to be dragged by its lower right edge. I guess that it is best to calculate its size as you create it and  then refer to it as you drag it.

    Sincerely yours,
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top