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

Delete A Bound Node and Refresh

2 Answers 142 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
heavywoody
Top achievements
Rank 1
heavywoody asked on 21 Jan 2011, 03:49 PM
I followed the example of using a self joined hierarchtical treeview using your DataItemCollection and extending my Entity to include SetOwner, Children, etc.  You showed me in this post http://www.telerik.com/community/forums/wpf/treeview/persist-item-order.aspx.  That all works perfectly!  What I am unsure of is how to delete a bound child then.  I thought if I just handled the ObservableCollection.CollectionChanged and removed the item from the underlining datasource it would update the visual tree, but it doesnt.  Here is what I am doing:

private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var selcontainer = treeColumns.SelectedContainer;
                WorkFlow selitem = treeColumns.SelectedItem as WorkFlow;

                _collection.Remove(selitem);
            }
          }

I also handled the CollectedChanged event:

    void _collection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            try
            {

                if (e.Action == NotifyCollectionChangedAction.Remove)
                {
                    foreach (object geoobject in e.OldItems)
                    {
                        ctx.WorkFlows.DeleteObject((WorkFlow)geoobject);
                    }
                }

                ctx.SaveChanges();
            }
            catch (Exception ex)
            {
                Helper.SetError(ex);
            }
        }

What do I need to do to actually refresh the treeview so that the object is gone, or should I be taking another approach?

2 Answers, 1 is accepted

Sort by
0
heavywoody
Top achievements
Rank 1
answered on 21 Jan 2011, 05:26 PM
I seem to have found a way to get this done, although not sure it is the best way.  However, if I try to loop through all SelectedItems and delete using this same way, I get an error saying the IEnumerable collection has changed.  But here is what I did to remove:

 

 

 

WorkFlow selWorkFlow = treeColumns.SelectedItem as WorkFlow;
RadTreeViewItem selParent = treeColumns.SelectedContainer.ParentItem as RadTreeViewItem;
WorkFlow parentWorkFlow = selParent.Item as WorkFlow;
//Remove from parents Children ObservableCollection
parentWorkFlow.Children.Remove(selWorkFlow);
//Remove from the Treeview ObservableCollection DataSource
_collection.Remove(selWorkFlow);

Would this be the best way of doing this? And how to extent this to do all SelectedItems, since the user might delete many items

 

0
Petar Mladenov
Telerik team
answered on 26 Jan 2011, 07:59 PM
Hi Christian Loepp,

I prepared a sample for you demonstrating the desired behavior. Please check it out and let me know if it satisfies you or not. Basically, I make a List of all Selected DataItem. Iterating over it, i check whether the current item is a root-level item or non-root. If it is a non-root item, i follow your approach. If it is root level item, i remove the item directly from the ItemsSource collection of the tree.

All the best,
Petar Mladenov
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
Tags
TreeView
Asked by
heavywoody
Top achievements
Rank 1
Answers by
heavywoody
Top achievements
Rank 1
Petar Mladenov
Telerik team
Share this question
or