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

Removing Items From TreeView

6 Answers 780 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Burke
Top achievements
Rank 1
Burke asked on 04 Jun 2009, 05:54 PM
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); 

6 Answers, 1 is accepted

Sort by
0
Miroslav
Telerik team
answered on 05 Jun 2009, 11:38 AM
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.
0
Burke
Top achievements
Rank 1
answered on 05 Jun 2009, 03:33 PM
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!
0
pavel
Top achievements
Rank 1
answered on 23 Apr 2012, 12:57 PM
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);

submitchanges
0
Tina Stancheva
Telerik team
answered on 26 Apr 2012, 10:03 AM
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 >>

0
pavel
Top achievements
Rank 1
answered on 27 Apr 2012, 05:34 PM
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 


сбор
имя существительное коллекция, сбор, набор, собрание, инкассо, взыскание, взимание, собираемость, собирание, инкассация, скопление, инкассирование, толпа, денежный сбор, получение денег, взимание платы, денежные пожертвования, денежное пожертвование, экзамены в конце семестра, операции по инкассации
имя прилагательное коллекционный, инкассовый
0
Tina Stancheva
Telerik team
answered on 30 Apr 2012, 01:49 PM
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 >>

Tags
TreeView
Asked by
Burke
Top achievements
Rank 1
Answers by
Miroslav
Telerik team
Burke
Top achievements
Rank 1
pavel
Top achievements
Rank 1
Tina Stancheva
Telerik team
Share this question
or