NullReferenceException - Drag & Drop with ICollectionView

4 posts, 0 answers
  1. Bruno
    Bruno avatar
    54 posts
    Member since:
    Jul 2012

    Posted 24 May 2012 Link to this post

    Hi Telerik Team,

    In my Application, I've a RadTreeView which is bound to a ObservableCollection. That I could sorting the entries I have implemented a Converter in the ItemsSource of the RadTreeView and in the ItemsSource of the HierarchicalDataTemplate.

    Converter:
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
          {
             IEnumerable<object> counties = value as IEnumerable<object>;
             CollectionViewSource cvs = new CollectionViewSource();
             cvs.Source = counties;
             cvs.SortDescriptions.Add(new SortDescription(parameter.ToString(), ListSortDirection.Ascending));
             return cvs.View;
          }

    If I enable DragDrop on the RadTreeView (IsDragDropEnabled = true) and I Drag & Drop an Entry, I receive a NullReferenceException:

    Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.

    Details:
       bei Telerik.Windows.Controls.RadTreeView.EndDrop(FrameworkElement dropTarget, IEnumerable draggedItemsEnumerable)
       bei Telerik.Windows.Controls.RadTreeView.OnDropComplete(FrameworkElement dropDestination, IEnumerable draggedItems)
       bei Telerik.Windows.Controls.RadTreeView.OnTreeViewDropInfo(Object sender, DragDropEventArgs e)
       bei Telerik.Windows.Controls.DragDrop.DragDropEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       bei Telerik.Windows.RadRoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       bei Telerik.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RadRoutedEventArgs routedEventArgs)
       bei Telerik.Windows.RouteItem.InvokeHandler(RadRoutedEventArgs routedEventArgs)
       bei Telerik.Windows.EventRoute.InvokeHandlersImpl(Object source, RadRoutedEventArgs args, Boolean raisedAgain)
       bei Telerik.Windows.EventRoute.InvokeHandlers(Object source, RadRoutedEventArgs args)
       bei Telerik.Windows.RadRoutedEventHelper.RaiseEvent(DependencyObject element, RadRoutedEventArgs args)
       bei Telerik.Windows.DependencyObjectExtensions.RaiseEvent(DependencyObject element, RadRoutedEventArgs e)
       bei Telerik.Windows.Controls.DragDrop.RadDragAndDropManager.DragDropProvider_DropInfo(Object sender, DragDropEventArgs e)
       bei Telerik.Windows.Controls.DragDrop.DragDropProviderBase.RaiseDropInfo()
       bei Telerik.Windows.Controls.DragDrop.DragDropProvider.FinishDrag()
       bei Telerik.Windows.Controls.DragDrop.DragDropProvider.OnElementDrop(Object sender, DragEventArgs e)
       bei Telerik.Windows.DragDrop.DragEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       bei Telerik.Windows.RadRoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       bei Telerik.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RadRoutedEventArgs routedEventArgs)
       bei Telerik.Windows.RouteItem.InvokeHandler(RadRoutedEventArgs routedEventArgs)
       bei Telerik.Windows.EventRoute.InvokeHandlersImpl(Object source, RadRoutedEventArgs args, Boolean raisedAgain)
       bei Telerik.Windows.EventRoute.InvokeHandlers(Object source, RadRoutedEventArgs args)
       bei Telerik.Windows.RadRoutedEventHelper.RaiseEvent(DependencyObject element, RadRoutedEventArgs args)
       bei Telerik.Windows.DependencyObjectExtensions.RaiseEvent(DependencyObject element, RadRoutedEventArgs e)
       bei Telerik.Windows.DragDrop.DragOperation.RaiseDragEvent(RoutedEvent dragEvent, DependencyObject target)
       bei Telerik.Windows.DragDrop.DragOperation.OnDrop(DependencyObject source)
       bei Telerik.Windows.DragDrop.DragOperation.DragSourceLostMouseCapture(Object sender, MouseEventArgs e)
       bei MS.Internal.CoreInvokeHandler.InvokeEventHandler(UInt32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
       bei MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName, UInt32 flags)

    Kind regards,
    Bruno

  2. Hristo
    Admin
    Hristo avatar
    352 posts

    Posted 29 May 2012 Link to this post

    Hi,

    I would like to recommend you to sort you collection before giving it to the binding and not sorting it in the converter. Bindings and converters are harder to debug that plain collections in the view model and may cost you some time in the future.

    However, I've created a sample solution to simulate your issue, but with no avail. Could you please take a look at the attached project and modify it in order to introduce the issue.

    Regards,
    Hristo
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
  4. Bruno
    Bruno avatar
    54 posts
    Member since:
    Jul 2012

    Posted 29 May 2012 Link to this post

    Hi Hristo,

    thank you for your response.
    You have also to put the converter in the ItemsSource of the HierarchicalDataTemplate (the same as in the ItemsSource of the TreeView).
    Then in the CodeBehind you have to insert a handler for the DropQueryEvent:
    public MainPage()
    {
       InitializeComponent();
     
       this.sampleVM = new SampleViewModel();
       this.myTreeView.DataContext = this.sampleVM;
       this.myTreeView.AddHandler(RadDragAndDropManager.DropQueryEvent, new EventHandler<DragDropQueryEventArgs>(OnmyTreeView_DropQuery), true);
    }
     
    private void OnmyTreeView_DropQuery(object sender, DragDropQueryEventArgs e)
    {
       e.QueryResult = true;
    }

    Now you should also receive the exception.

    Kind regards,
    Bruno
  5. Hristo
    Admin
    Hristo avatar
    352 posts

    Posted 30 May 2012 Link to this post

    Hi,

    Thank you for the additional information.
    The TreeView internally performs a check for the items source type in order to perform some add/remove logic. This is poor design feature that we should change in future. As you could guess the control does not support ListCollectionView type for its item source so there is the null reference exception. Firing this exception is a bug in the control. Using the DropQueryEvent and setting the result to true overrides the default TreeView behavior and changes the code flow.

    Unfortunately, simply not firing the exception won't allow you to use the built-in drag and drop logic when using converter in the items source binding.
    I would like to suggest you to either not use the converted in the binding and sort the collection at view-model level, or implement your custom drag and drop logic (which is very cumbersome and time consuming).

    Regards,
    Hristo
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top