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

NullReferenceException - Drag & Drop with ICollectionView

3 Answers 126 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Bruno
Top achievements
Rank 1
Bruno asked on 24 May 2012, 03:58 PM
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

3 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 29 May 2012, 08:21 AM
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 >>

0
Bruno
Top achievements
Rank 1
answered on 29 May 2012, 09:45 AM
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
0
Hristo
Telerik team
answered on 30 May 2012, 10:54 AM
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 >>

Tags
TreeView
Asked by
Bruno
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Bruno
Top achievements
Rank 1
Share this question
or