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

Drag & drop + databinding

9 Answers 193 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
James asked on 03 Apr 2009, 05:24 PM
Hi,

Is there an example of how to implement drag & drop with a databound tree? I have a set of objects which contain lists of objects which contain lists (etc) and, when bound to the tree, give the correct hierarchical display.

What happens to the data collection when one item is dragged and dropped on another? I can see that the dragged item is deleted from the list it originated in (I think), but am not sure what what is called in the recieving list. I need to handle these changes to send them to the database.

(At the moment, one drag & drop works ok, but if I try to drag and drop the same node again, I end up with duplicate nodes. I think this is because the databinding is getting out of date, with the tree using an old version of the databound object (when an object is updated/saved it returns a new object to be bound)).

Thanks for any guidance, James.

9 Answers, 1 is accepted

Sort by
0
Accepted
Miroslav
Telerik team
answered on 04 Apr 2009, 09:47 AM
Hi James,

The RadTreeView will try to cast the ItemsSource object to a collection and add/remove items.

The ItemsControls can only properly update the presentation if the bound collection implement INotifyColelctionChanged (as the built-in ObservableCollection<T>) and INotifyPropertyChanged. Please make sure that your collections are observable and the TreeView should handle the presentation correctly.

Also, you can use these interfaces to be notifies whenever items are added/removed. Alternatively, you can handle the DragEnded event of the TreeView, where you will have a collection of the dragged items.

If you are using observable collections and you still encounter presentation issues, please come back to us and we will look into the reasons.

Regards,
Miroslav
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
James
Top achievements
Rank 1
answered on 08 Apr 2009, 11:29 AM
Hi, thanks for your answer. I've got it working pretty well now, but am hitting an exception that I think might be a bug in the treeview (though am not sure). I'm handling all the changes to the objects myself, so am setting e.Handled = true in PreviewDragEnded. I'm using PreviewDragEnded, rather than DragEnded, as setting e.Handled = true there doesn't cancel the drag. I want the drag canceled as I then move the objects within my bound collections myself and allow the CollectionChanged event to notify the treeview and have it update its appearance. So far so good. BUT after a few drags and drops of the same items between one another, I get an exception:

   at Telerik.Windows.Controls.RadTreeViewItem.PrepareContainerForItemOverride(DependencyObject element, Object item)
   at System.Windows.Controls.ItemsControl.UpdateContainerForItem(Int32 index)
   at System.Windows.Controls.ItemsControl.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)

I have:
root - item 1
       - item 2 - item 5 - item 6
       - item 3 - item 7
       - item 4

As far as I can see in the debugger, my objects look fine. I've been dragging and dropping items 5 to 7 around (i.e. not root objects). I can drag and drop one item as much as I like - into and out of other nodes and it's fine. The problems start when I start putting items that have already been dragged and dropped into one another - after a few iterations I get the exception - but I can't see a pattern.

The fact that there's no obvious pattern and because my objects look just the same, whether or not an exception is about to occur, has left me stumped. Any help is very welcome!

Thanks, James

** Editing just to say that the exception is:
System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."
0
Bobi
Telerik team
answered on 10 Apr 2009, 11:55 AM
Hello James,

Thank you for reporting this issue. We will fix it as soon as possible.
Your account is already updated  with 1000 Telerik points.

Best wishes,
Boryana
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
James
Top achievements
Rank 1
answered on 27 May 2009, 10:06 PM
Hi Boryana,

Is there any news on fixing this? I've just tried with the latest internal build and am getting the same error.

Thanks for your help, James.

(This is an absolute showstopper for me at the moment, and is delaying me being able to deploy my application.)
0
Miroslav
Telerik team
answered on 28 May 2009, 06:37 AM
Hi James,

To best of my knowledge we reproduced and fixed this issue, I checked that we also have a unit test running for this. The issue arised from a bug in the ItemsControl which resulted in an unexpected calls to the Prepare/Clear container methods when an object was placed as a child of its former child.

The project I used to reproduce this was based on your example. Before it would always break if you drag 5 into 7, 5 into 1 and breaking it with 7 into 5. I am attaching it here as well.

With the latest dlls I am not able to break this anymore. I would do my best to resolve this as quickly as possible if there is still an issue there.

Could you:
- Check that you are indeed using the latest assemblies?
- Download the project and see if it breaks there?
- If you still have the issue, can you confirm that the StackTrace for the error is the same.
- Tell us if there is anything that I am missing, could you break the example?

I am sorry for delaying you, we would send you a hotfix once we get to the problem.

Best wishes,
Miroslav
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.
0
James
Top achievements
Rank 1
answered on 28 May 2009, 04:04 PM
Hi Miroslav,

Sorry - my mistake. For some reason VS hasn't been updating its references to Telerik dlls when I install new versions of them. I confirm that the fix is working and I can't get the same error any more. (Now there's no excuse for me not to deploy the app!)

Thanks for your help and sorry for the false alarm.

James.
0
Gaurang
Top achievements
Rank 1
answered on 07 Sep 2011, 10:21 AM
Hi,

My problem is that 
1) When I try to add new node first time it display propelry and then try to add second new node or one words it does not show me any new node insted of that it show me only blank spalce/gap.
2) If I try to delete node from root then I deleted properly but If I try to delete subnode it deleted form database and treeviews objects but client side it remains as it is.
3) If I try to drag and drop one node to another node that time it also does not move but it remains as it is, but in database and treeview objects all changed as I need.

If I try to add(more thand one)/remove/drag-drop a node it dones show me effects, but if I close the page and open it shows me the all changes. I have post my issue
http://www.telerik.com/community/forums/silverlight/treeview/problem-for-dropping-node-and-insertion-more-than-one-node.aspx

My XAML RadTreeView
<telerik:ContainerBindingCollection x:Key="treeViewItemBindings">
        </telerik:ContainerBindingCollection>
        <telerik:HierarchicalDataTemplate x:Key="treeViewTemplate" ItemsSource="{Binding ChildFolder}"
                telerik:ContainerBinding.ContainerBindings="{StaticResource treeViewItemBindings}">
            <StackPanel Orientation="Horizontal">
                <Image x:Name="FolderImage" Source="{Binding FolderImage, Converter={StaticResource byteArrayToImageConverter}}" Margin="0,0,5,0"/>
               <TextBlock FontStyle="{Binding FontStyle}"
                      Foreground="{Binding Foreground}" Text="{Binding FolderName}" />
           </StackPanel>
        </telerik:HierarchicalDataTemplate>
        <Style TargetType="RadNavigation:RadTreeViewItem" x:Key="TreeViewItemStyle">
        </Style>
 
---------------------------------------------------------------------------------------------
 
<RadNavigation:RadTreeView x:Name="FolderTreeview" HorizontalAlignment="Stretch"
  Margin="5,0,0,0" VerticalAlignment="Stretch" IsDragDropEnabled="True"
  IsLineEnabled="True" IsRootLinesEnabled="True"
  ItemTemplate="{StaticResource treeViewTemplate}"
  ItemContainerStyle="{StaticResource TreeViewItemStyle}"
  SelectionMode="Single" Width="Auto" Height="Auto">
</RadNavigation:RadTreeView>


Please reply me.

Thanks,
Gaurang
0
Tina Stancheva
Telerik team
answered on 12 Sep 2011, 03:27 PM
Hi Gaurang,

Can you please have a look at the other thread you started and take a few minutes to answer a few questions for us. This is important to us as we'll highly appreciate your feedback  on the show-stoppers that you faced through the time you have been evaluating our controls.

Thank you in advance for your cooperation.

Greetings,
Tina Stancheva
the Telerik team

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

0
Gaurang
Top achievements
Rank 1
answered on 13 Sep 2011, 11:48 AM
Hi,

I resolved my issue. For that I created a new partial ObservableCollection class at client side. Before that I created only one class server side. Now I have the same class server and client both side.

Thanks,
Gaurang
Tags
TreeView
Asked by
James
Top achievements
Rank 1
Answers by
Miroslav
Telerik team
James
Top achievements
Rank 1
Bobi
Telerik team
Gaurang
Top achievements
Rank 1
Tina Stancheva
Telerik team
Share this question
or