Drag from RadTreeView to RadGridView - TreeViewDragDropOptions always null

2 posts, 0 answers
  1. Mike
    Mike avatar
    1 posts
    Member since:
    Apr 2017

    Posted 06 Apr Link to this post

    I need to determine if the issue is my version of the implementation of drag/drop or if I am missing something in code.  The version we are using is 2013.3.1204.1050 (I realize it is old however very stable for us).

    The issue I am having is on the grid's drag/drop behavior, the TreeViewDragDropOptions returns back as null when dragged from the TreeView to the grid.  I have no idea why when the argument's data property contains the object being dragged over to the grid.  Any help would be appreciative.

    XAML:

    Resources -

    <DataTemplate x:Name="ShiftRouteRun">
        <StackPanel>
            <TextBlock Text="{Binding RouteRunCode}" />
        </StackPanel>
    </DataTemplate>
     
    <telerik:HierarchicalDataTemplate x:Name="ShiftPlanTemplate"
                                      ItemsSource="{Binding RouteRuns}"
                                      ItemTemplate="{StaticResource ShiftRouteRun}">
        <StackPanel>
            <TextBlock Text="{Binding ShiftTimeDescription}">
                <telerik:RadContextMenu.ContextMenu>
                    <telerik:RadContextMenu Opened="RadContextMenu_Opened">
                        <telerik:RadMenuItem Header="Edit" />
                    </telerik:RadContextMenu>
                </telerik:RadContextMenu.ContextMenu>
            </TextBlock>
        </StackPanel>
    </telerik:HierarchicalDataTemplate>

     

    <telerik:RadTreeView x:Name="tvShifts"
                                         ItemTemplate="{StaticResource ShiftPlanTemplate}"
                                         IsEditable="True"
                                         ItemEditTemplate="{StaticResource ShiftEditTemplate}"
                                         IsDragDropEnabled="True"
                                         Grid.Column="0"
                                         Grid.Row="1" />
     
    <telerik:RadGridView x:Name="dgAvailableRuns"
                                         RowStyle="{StaticResource GridDropStyle}"
                                         AllowDrop="True"
                                         AutoGenerateColumns="False"
                                         IsReadOnly="True"
                                         SelectionMode="Single"
                                         ShowGroupPanel="False"
                                         RowIndicatorVisibility="Collapsed"
                                         IsSynchronizedWithCurrentItem="false"
                                         FrozenColumnCount="1"
                                         Height="200"
                                         local:RouteShiftPlanGridViewDropBehavior.IsEnabled="True"
                                         Grid.Column="1"
                                         Grid.Row="1">
                        <telerik:RadGridView.Resources>
                            <DataTemplate x:Key="DragItemTemplate">
                                <StackPanel>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="Dragging:" />
                                        <TextBlock Text="{Binding RouteRunCode}"
                                                   FontWeight="Bold" />
                                    </StackPanel>
                                </StackPanel>
                            </DataTemplate>
                        </telerik:RadGridView.Resources>
                        <telerik:RadGridView.Columns>
                            <telerik:GridViewDataColumn ShowDistinctFilters="False"
                                                        DataMemberBinding="{Binding RouteRunCode}" />
                            <telerik:GridViewDataColumn ShowDistinctFilters="False"
                                                        DataMemberBinding="{Binding SchoolDescription}" />
                            <telerik:GridViewDataColumn ShowDistinctFilters="False"
                                                        DataMemberBinding="{Binding DistrictDescription}" />
                            <telerik:GridViewDataColumn ShowDistinctFilters="False"
                                                        DataMemberBinding="{Binding CalendarDescription}" />
                        </telerik:RadGridView.Columns>
                    </telerik:RadGridView>

     

    Behavior:

    Imports Telerik.Windows.Controls
    Imports Telerik.Windows.DragDrop
    Imports System.Collections
     
    ''' <summary>
    ''' For drag/drop handlers on a RadGridView control
    ''' </summary>
    Public Class RouteShiftPlanGridViewDropBehavior
     
        Private _associatedObject As RadGridView
        Private Shared instances As Dictionary(Of RadGridView, RouteShiftPlanGridViewDropBehavior)
     
    #Region " Constructors "
        Shared Sub New()
            instances = New Dictionary(Of RadGridView, RouteShiftPlanGridViewDropBehavior)()
        End Sub
    #End Region
     
    #Region " Public Properties "
        ''' <remarks>For binding to a RadGridView control</remarks>
        Public Shared ReadOnly IsEnabledProperty As DependencyProperty =
            DependencyProperty.RegisterAttached("IsEnabled", GetType(Boolean), GetType(RouteShiftPlanGridViewDropBehavior), New PropertyMetadata(New PropertyChangedCallback(AddressOf OnIsEnabledPropertyChanged)))
     
        ''' <summary>
        ''' AssociatedObject Property
        ''' </summary>
        Public Property AssociatedObject As RadGridView
            Get
                Return _associatedObject
            End Get
            Set(value As RadGridView)
                _associatedObject = value
            End Set
        End Property
    #End Region
     
    #Region " Methods "
        Public Shared Function GetIsEnabled(obj As DependencyObject) As Boolean
            Return CBool(obj.GetValue(IsEnabledProperty))
        End Function
     
        Public Shared Sub OnIsEnabledPropertyChanged(dependencyObject As DependencyObject, e As DependencyPropertyChangedEventArgs)
            SetIsEnabled(dependencyObject, CBool(e.NewValue))
        End Sub
     
        Public Shared Sub SetIsEnabled(obj As DependencyObject, value As Boolean)
            Dim behavior As RouteShiftPlanGridViewDropBehavior = GetAttachedBehavior(TryCast(obj, RadGridView))
            behavior.AssociatedObject = TryCast(obj, RadGridView)
     
            If value Then
                behavior.Initialize()
            Else
                behavior.CleanUp()
            End If
     
            obj.SetValue(IsEnabledProperty, value)
        End Sub
     
        Private Shared Function GetAttachedBehavior(gridView As RadGridView) As RouteShiftPlanGridViewDropBehavior
            If Not instances.ContainsKey(gridView) Then
                instances(gridView) = New RouteShiftPlanGridViewDropBehavior()
                instances(gridView).AssociatedObject = gridView
            End If
     
            Return instances(gridView)
        End Function
     
        Protected Overridable Sub Initialize()
            UnsubscribeFromDragDropEvents()
            SubscribeToDragDropEvents()
        End Sub
     
        Protected Overridable Sub CleanUp()
            Me.UnsubscribeFromDragDropEvents()
        End Sub
     
        Private Sub SubscribeToDragDropEvents()
            DragDropManager.AddDragInitializeHandler(Me.AssociatedObject, AddressOf OnDragInitialize)
            DragDropManager.AddGiveFeedbackHandler(Me.AssociatedObject, AddressOf OnGiveFeedback)
            DragDropManager.AddDropHandler(Me.AssociatedObject, AddressOf OnDrop)
            DragDropManager.AddDragDropCompletedHandler(Me.AssociatedObject, AddressOf OnDragDropCompleted)
            DragDropManager.AddDragOverHandler(Me.AssociatedObject, AddressOf OnDragOver)
        End Sub
     
        Private Sub UnsubscribeFromDragDropEvents()
            DragDropManager.RemoveDragInitializeHandler(Me.AssociatedObject, AddressOf OnDragInitialize)
            DragDropManager.RemoveGiveFeedbackHandler(Me.AssociatedObject, AddressOf OnGiveFeedback)
            DragDropManager.RemoveDropHandler(Me.AssociatedObject, AddressOf OnDrop)
            DragDropManager.RemoveDragDropCompletedHandler(Me.AssociatedObject, AddressOf OnDragDropCompleted)
            DragDropManager.RemoveDragOverHandler(Me.AssociatedObject, AddressOf OnDragOver)
        End Sub
     
        Private Sub OnDragInitialize(sender As Object, args As DragInitializeEventArgs)
            Dim data As RouteRun = TryCast(DirectCast(args.OriginalSource, FrameworkElement).DataContext, RouteRun)
     
            If data Is Nothing Then
                args.Cancel = True
                Return
            End If
     
            Dim details As New DropIndicatorHelper()
            details.CurrentDraggedItem = data
     
            Dim payload As Telerik.Windows.DragDrop.Behaviors.IDragPayload = Telerik.Windows.DragDrop.DragDropPayloadManager.GeneratePayload(Nothing)
            payload.SetData(GetType(RouteRun), data)
            payload.SetData("DropDetails", details)
     
            Dim visual As New ContentControl()
            With visual
                .Content = data
                .ContentTemplate = TryCast(Me.AssociatedObject.Resources("DragItemTemplate"), DataTemplate)
            End With
     
            args.Data = payload
            args.DragVisual = visual
            args.DragVisualOffset = args.RelativeStartPoint
            args.AllowedEffects = DragDropEffects.All
        End Sub
     
        Private Sub OnGiveFeedback(ByVal sender As Object, ByVal args As Telerik.Windows.DragDrop.GiveFeedbackEventArgs)
            args.SetCursor(Cursors.Hand)
            args.Handled = True
        End Sub
     
        Private Sub OnDrop(sender As Object, e As Telerik.Windows.DragDrop.DragEventArgs)
            Dim options As TreeView.TreeViewDragDropOptions = TryCast(DragDropPayloadManager.GetDataFromObject(e.Data, TreeView.TreeViewDragDropOptions.Key), TreeView.TreeViewDragDropOptions)
            If options Is Nothing Then
                Return
            End If
     
            Dim draggedItem = options.DraggedItems.FirstOrDefault()
            If draggedItem Is Nothing Then
                Return
            End If
     
            If e.Effects <> DragDropEffects.None Then
                Dim collection As IList = TryCast(TryCast(sender, RadGridView).ItemsSource, IList)
                collection.Add(draggedItem)
            End If
     
            e.Handled = True
        End Sub
     
        Private Sub OnDragDropCompleted(sender As Object, e As DragDropCompletedEventArgs)
            Dim draggedItem = DragDropPayloadManager.GetDataFromObject(e.Data, GetType(RouteRun))
     
            If e.Effects <> DragDropEffects.None Then
                Dim collection As IList = TryCast(TryCast(sender, RadGridView).ItemsSource, IList)
                collection.Remove(draggedItem)
            End If
        End Sub
     
        Private Sub OnDragOver(sender As Object, e As Telerik.Windows.DragDrop.DragEventArgs)
            Dim options As TreeView.TreeViewDragDropOptions = TryCast(DragDropPayloadManager.GetDataFromObject(e.Data, TreeView.TreeViewDragDropOptions.Key), TreeView.TreeViewDragDropOptions)
            If options Is Nothing Then
                e.Effects = DragDropEffects.None
                e.Handled = True
                Return
            End If
     
            Dim draggedItem = options.DraggedItems.FirstOrDefault()
            Dim itemsType As Type = TryCast(Me.AssociatedObject.ItemsSource, IList).AsQueryable().ElementType
     
            If draggedItem.[GetType]() IsNot itemsType Then
                e.Effects = DragDropEffects.None
            Else
                TryCast(options.DragVisual, TreeView.TreeViewDragVisual).IsDropPossible = True
                options.DropAction = TreeView.DropAction.Move
                options.UpdateDragVisual()
            End If
     
            e.Handled = True
        End Sub
    #End Region
     
    End Class
  2. Polya
    Admin
    Polya avatar
    243 posts

    Posted 11 Apr Link to this post

    Hello Mike,

    We have a great example about drag and drop between RadTreeView and RadGridView in our QSF examples: http://demos.telerik.com/silverlight/#DragAndDrop/TreeToGrid.
    In this example the extracted object from the DragDropPayloladManager is of type DropIndicationDetails that holds information about the dragged item as well. You can try and use it instead of TreeViewDragDropOptions.
    Hope this helps.

    Regards,
    Polya
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top