How to get underlying object... always null

2 posts, 0 answers
  1. Nigel Shaw
    Nigel Shaw avatar
    4 posts
    Member since:
    Sep 2005

    Posted 06 Mar 2010 Link to this post

    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?
  2. Tina Stancheva
    Tina Stancheva avatar
    3299 posts

    Posted 10 Mar 2010 Link to this post

    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;

    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.
Back to Top