This question is locked. New answers and comments are not allowed.
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.ControlsImports Telerik.Windows.DragDropImports 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 RegionEnd Class