Delete A Bound Node and Refresh

3 posts, 0 answers
  1. heavywoody
    heavywoody avatar
    79 posts
    Member since:
    Oct 2009

    Posted 21 Jan 2011 Link to this post

    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. heavywoody
    heavywoody avatar
    79 posts
    Member since:
    Oct 2009

    Posted 21 Jan 2011 Link to this post

    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

     

  3. UI for WPF is Visual Studio 2017 Ready
  4. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 26 Jan 2011 Link to this post

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