Removing Items From TreeView

7 posts, 0 answers
  1. Burke
    Burke avatar
    41 posts
    Member since:
    Jun 2009

    Posted 04 Jun 2009 Link to this post

    I have a LINQ to SQL Object in an ObservableCollection.  It has child objects which in turn have child objects of their own.  This structure mimics a recursive join on the table.

    My issue is that when I remove a control from one of the child collections, it does not update the treeview.

    i.e.

    var itemToDelete = (Item)treeview1.SelectedItem;  
    var parentItem = (Item)controlToDelete.ParentItem;  
     
    parentItem.Remove(itemToDelete); 

    If I remove the item from the collection itself - that does not appear to work either.

    var itemToDelete = (Item) treeview1.SelectedItem;  
    var collection = ObservableCollection<Item> treeview1.ItemsSource;  
     
    collection.Remove(itemToDelete); 
  2. Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 05 Jun 2009 Link to this post

    Hello Thomas,

    I am quite surprised that the TreeView will not react to an observable items source change. I could not reproduce this on our side.

    Could you confirm that the children collections of the items you bind to are observable as well?

    If you have created the tree view hierarchy with some kind of LINQ data shaping (grouping) the resultant collection may not be observable.

    Could you share some more code (a bit of xaml and how you create the collections at least) or best send us a sample project where the TreeView will not update?

    All the best,
    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.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Burke
    Burke avatar
    41 posts
    Member since:
    Jun 2009

    Posted 05 Jun 2009 Link to this post

    I suppose what is confusing me is the LINQ structure.  The query autmatically retrieves child objects in a self association of type

    EntitySet<ChildItem> 

    I suppose what I am confused about is how to get the child objects into observable collections and still store them in the parent LINQ object.

    I guess I need to move this to the LINQ forums.

    Thanks!
  5. pavel
    pavel avatar
    15 posts
    Member since:
    Jul 2012

    Posted 23 Apr 2012 Link to this post

    Hello, I have the same problem.
    Do not update the contents of the collection in the "treeview".
    when adding and removing items. 

    TreeView Binding to "LINQ to SQL"

    <telerik:RadTreeView Name="treeView"
            Margin="0"
    HorizontalAlignment="Stretch"
    VerticalAlignment="Stretch"
    IsExpandOnSingleClickEnabled="True"
    ItemsOptionListType="None"
    Selected="treeView_Selected"
    SelectionMode="Single">
    <telerik:RadTreeView.ItemTemplate>
    <HierarchicalDataTemplate x:Name="item"
    ItemsSource="{Binding children, Mode=TwoWay}">
    <telerik:RadTreeViewItem Tag="{Binding}">
    <telerik:RadTreeViewItem.Template>
    <ControlTemplate>
    <StackPanel Orientation="Horizontal" >
    <Image Width="16" Height="16"
    Source="/Ric.Itsmc.WC.Filter_Reporting;component/Images/Folder.png" 
    ToolTip="{Binding description,
                                            Mode=TwoWay}" />
    <TextBlock Text="{Binding Path=title, Mode=TwoWay}" ToolTip="{Binding description, Mode=TwoWay}" />
    </StackPanel>
    </ControlTemplate>
    </telerik:RadTreeViewItem.Template>
    </telerik:RadTreeViewItem>
    </
    HierarchicalDataTemplate>
    </telerik:RadTreeView.ItemTemplate>
    </telerik:RadTreeView>

    binding to LINQ to SQL.

    Colums DataBase SQL 
    ID
    parent_ID,
    title,
    description,
    Forent Key - Parent_ID to ID - One to many.
     
     
            private void RadTreeViewItemAdd_ButtonClick(object sender, RoutedEventArgs e)
            {
             RadTreeViewItem treeViewItem = treeView.SelectedContainer as RadTreeViewItem;
              TreeItem  tf = treeViewItem.Tag as TreeItem ;
              if (tf == null) return;
              TreeItem filter = new TreeItem() { title = "New node " + DateTime.Now.ToShortDateString(), description = "*" +DateTime.Now.ToString() };
              tf.children.Add(filter);
              ef_report.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
        }
    // Item did not view treeView, item added.
     
            private void RadTreeViewItemRemove_ButtonClick(object sender, RoutedEventArgs e)
    }
         RadTreeViewItem treeViewItem = treeView.SelectedContainer as RadTreeViewItem;
      TreeItem  tf = treeViewItem.Tag as TreeItem ;
      if (tf == null) return;
      TreeItem item= new TreeItem() { title = "New node " + DateTime.Now.ToShortDateString(), description = "*" +DateTime.Now.ToString()};
    TreeItem  item_parent = item.parent;
    item_parent.children.Remove(item);
    linq_data_model.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
    }
    //Exteption "SubmitChanges" didn't remove ForentKey. 


    Linq_model linq_data_model = new Linq_model("connetion string");
    treeView.ItemsSource = linq_data_model.TreeItem.Where(w => w != null && w.parent_ID == 0 && w.ID != 0);

  6. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 26 Apr 2012 Link to this post

    Hello,

    In order to add/remove items in a databound RadTreeView, you need to bind its ItemsSource to a collection that implements the INotifyCollectionChanged interface. The ObservableCollection<T> has a built-in implementation of a data collection that exposes the INotifyCollectionChanged interface. However, your LINQ statement returns a collection that doesn't implement the interface. Therefore when you use a LINW to SQL to set the RadTreeView ItemsSource collection, the RadTreeView control won't be notified if the collection is changed.

    Basically the RadTreeView control is a data-driven control. So the RadTreeViewItems add/remove operations are changing the underlying ItemsSource collection in databound scenarios. Now, when you cannot notify the RadTreeView that the collection is changed, the control won't be able to visually display those changes thus causing the issues you experienced.

    In order to get over this behavior you can wrap the collection returned by the LINQ statement in an ObservableCollection - you can try something like that:
    var collection = linq_data_model.TreeItem.Where(w => w != null && w.parent_ID == 0 && w.ID != 0);
    treeView.ItemsSource =new ObservableCollection<T>(collection);

    If this doesn't help it would be best to send us a small sample reproducing your scenario and the issue so that we can further examine the case.

    All the best,
    Tina Stancheva
    the Telerik team

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

  7. pavel
    pavel avatar
    15 posts
    Member since:
    Jul 2012

    Posted 27 Apr 2012 Link to this post

    Thank you very much, it works if I add a collection into the main tree.
    But I have also a collection of nested collections of the same class.
    That is, in a database table key bindings closed its same table, one to many. Andnested collection Property EtitySet <TreeItem>, and not the ObservableCollection<TreeItem>. 

    http://i50.tinypic.com/24y4bqq.png 


  8. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 30 Apr 2012 Link to this post

    Hello Sergei,

    You'll need to encapsulate this collection in an ObservableCollection as well. For example you can create wrapper classes to wrap the data coming from your database. This way you can get the data from the tables but wrap it in your own business classes thus making sure all collections are observable.

    All the best,
    Tina Stancheva
    the Telerik team

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

Back to Top
UI for WPF is Visual Studio 2017 Ready