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

How to get underlying object... always null

1 Answer 69 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Nigel Shaw
Top achievements
Rank 1
Nigel Shaw asked on 06 Mar 2010, 04:44 PM
I'm trying to get the underlying object that's bound to a dropped item in a drag-drop operation. In my case the underlying object is of type Model.Topic.

I use the following code:

private void RadTreeView_DragEnded(object sender, Telerik.Windows.Controls.RadTreeViewDragEndedEventArgs e)  
        {  
            object droppedTopics = e.DraggedItems;  
              
            object targetDropItem = e.TargetDropItem;  
            Model.Topic targetTopic = targetDropItem as Model.Topic;  
 
            foreach (object droppedTopic in e.DraggedItems as Collection<object>)  
            {  
                Model.Topic sourceTopic = droppedTopic as Model.Topic;  
                targetTopic.ParentTopicId = sourceTopic.ID;  
            }  
        }  
 

The problem is that after the following line, targetTopic is always null.

Model.Topic targetTopic = targetDropItem as Model.Topic; 

The question is, what is the correct way to get the underlying object that is bound to a dragged/dropped item?

1 Answer, 1 is accepted

Sort by
0
Tina Stancheva
Telerik team
answered on 10 Mar 2010, 05:47 PM
Hi Nigel Shaw,

The targetTopic is null because e.TargetDropItem is the dynamically generated container for your business object. Therefore you need to cast it to the type of the container (RadTreeViewItem) like this:
     RadTreeViewItem container = e.TargetDropItem as RadTreeViewItem;
Afterwards, you can access the business object itself via the container.Item property:
    Model.Topic targetTopic = container.Item as Model.Topic;

Regards,
Tina Stancheva
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
TreeView
Asked by
Nigel Shaw
Top achievements
Rank 1
Answers by
Tina Stancheva
Telerik team
Share this question
or