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

Remove RadTreeViewItem

7 Answers 155 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Stephane Priolet
Top achievements
Rank 1
Stephane Priolet asked on 11 Dec 2008, 11:41 AM
Hi,

I'm trying to remove an item from my treeview. Items are added something like that :

foreach (<object> element in <CollectionObject>)  
{  
    mytreeview.Item.add(new RadTreeViewItem{Header = element.Title});  

So they are not added with ItemSource property.

I try to do  : 

mytreeview.Items.Remove(mytreeview.SelectedItem); 

And there is no effect, selecteditem stay here.

What forgotten i ? Thanks in advance.

7 Answers, 1 is accepted

Sort by
0
Stephane Priolet
Top achievements
Rank 1
answered on 11 Dec 2008, 02:44 PM
Another, important information : my treeview use LoadOnDemand method.
Delete an item like described upon works with children root elements but not with second hierarchical elements wich are loaded on demand.

|ROOT
|------ (LoadOnDemand) child root element #1 -> delete ok
|------|------ second hierarchical element #1.0 -> delete not ok
|------|------ second hierarchical element #1.1 -> delete not ok
|------ (LoadOnDemand) child root element #2 -> delete ok
|------|------ second hierarchical element #2.0 -> delete not ok
|------|------ second hierarchical element #2.1 -> delete not ok
|------|------ second hierarchical element #2.2 -> delete not ok

Thanks
0
Miroslav
Telerik team
answered on 11 Dec 2008, 04:35 PM
Hi Stephane,

The items of the TreeView are not a list of the whole hierarchy but only of its immediate children. So what you will need to do is remove the items from their respective parents. Visually the TreeView has TreeViewItems for children, then these TreeViewItems have Items collections of their own with other TreeViewItems inside.

There is a static function that may help you locate the parent of an item:

Telerik.Windows.Controls.ItemsControl.ItemsControlFromItemContainer(myItemContainer); 

In your case that will help you get the parent of the item and then remove it from there.

The reason why removing the Selected item works only sometimes is because when you select a root node, it can be removed and is ok, but when you select a descendant, its parent is not the TreeView but another TreeView item.

I hope this will help you achieve what you need,

Regards,
Miroslav
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Stephane Priolet
Top achievements
Rank 1
answered on 11 Dec 2008, 06:41 PM
Thanks, it's clear and now fully works
0
smith
Top achievements
Rank 1
answered on 29 Aug 2009, 08:15 AM
HI
 i am new for this MVVM and Telerik Treeview .
i am using Hierarchical treeview  with MVVM Pattern .  using Context menu remove the item from treeview. 


(rvwitwm.Parent as RadTreeViewItem).Items.Remove((tvwFBDiagram.SelectedItem as NodeItem).NodeText);

it is not work ing got the error Parent is null
how to delete the Item from treeview if particular element got child  no need to delete it should add previous parent
How to do ?

also
in hierarchal datasource i need  to disable parent nodes .. 
please send one example for this ...

regrds
smith

0
Miroslav
Telerik team
answered on 31 Aug 2009, 02:14 PM
Hello smith,

You can get the items control for any item container by using the

ItemsControl.ItemsControlFromItemContainer

static method. The Parent property returns the parent only if the items have been defined in xaml. It does not work for bound items (this is inherited from the base ItemsControl and is not something that we can not change)

As for your second question - do you mean that you would not like to have parent nodes? In this case, you just need to use a regular DataTemplate or not set the ItemsSource property of the HierarchicalDataTemplate.

Then, if you would like to disable the parent items, then just set their IsEnabled property to false.

Kind regards,
Miroslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
smith
Top achievements
Rank 1
answered on 31 Aug 2009, 03:43 PM
HI

 I am nopt able to get
can u send me simple example for this one


one more question is

i need to check befor drag the object it i svalid or not. child elemnets can not be the parent element only drag should happened child element it self. not child to parent.

how to check Drag Befor Event. this is child or parent.,

thanks and regards
snith
0
Miroslav
Telerik team
answered on 02 Sep 2009, 12:08 PM
Hello smith,

It was not exactly clear what you would like to see as an example. I am sending you modification of one of the DragDrop samples that we have where you can see how to get the parent of an item and how to disable it.

You need to handle the DropQuery event of the TreeView. There you have both the source and the destination of the DragDrop and you can plugin your own rules for drag drop. You can extract the ViewModel from the DataContext in the container and you could possibly decide whether the drop is possible depending on it.

Search for EX: in the attached example for the lines that illustrate my reply.

Hopefully this will help you achieve the behavior that you need,

Best wishes,
Miroslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
TreeView
Asked by
Stephane Priolet
Top achievements
Rank 1
Answers by
Stephane Priolet
Top achievements
Rank 1
Miroslav
Telerik team
smith
Top achievements
Rank 1
Share this question
or