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

Check a Child Node and Expand all Parents

2 Answers 193 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
WILLIAM
Top achievements
Rank 1
WILLIAM asked on 08 Aug 2012, 11:27 PM
In my RadTree, there is a node that I need to be checked when the view initially shown.  I have been able to accomplish this part, but I have not been able to expand all of its parents.  For the parents, I have been able to set their checked state to intermediate and calling BringToView, but the parents are still not expanding.

I'm subscribing to the ItemPrepared event to select the checked state of the child and parents.

Wouldn't a method to expand the note be easier?  Item.Expand()

Also, I've read in other posts that you have to wait until the Items have been loaded, that makes sense, but what event do you subscribe to that lets you know that the items have been loaded?  ItemsLoaded event? or Items.CollectionChanged?

xaml
<telerik:ContainerBindingCollection x:Key="TreeViewItemContainerBinding">
    <telerik:ContainerBinding PropertyName="IsExpanded" Binding="{Binding Path=IsActive, Mode=TwoWay}" />
    <telerik:ContainerBinding PropertyName="IsSelected" Binding="{Binding Path=IsActive}" />
</telerik:ContainerBindingCollection>

<telerik:HierarchicalDataTemplate x:Key="OrgUnitsItemTemplate" ItemsSource="{Binding OrgUnits}"
     telerik:ContainerBinding.ContainerBindings ="{StaticResource TreeViewItemContainerBinding}">
     <StackPanel Orientation="Horizontal">
         <telerik:Label Content="{Binding ParentOrgUnit.Description}" />
     </StackPanel>
</telerik:HierarchicalDataTemplate>

void UxOrgUnitsItemPrepared(object sender, RadTreeViewItemPreparedEventArgs e)
        {
            var item = e.PreparedItem.Item as OrgUnitHierarchy;
            if (item == null || item.ParentOrgUnit.OrgUnitId != Dto.CurrentOrgUnitId) return;
 
            e.PreparedItem.CheckState = ToggleState.On;
 
            var parentItem = e.PreparedItem.ParentItem;
          
            while (parentItem != null)
            {
                parentItem.CheckState = ToggleState.Indeterminate;
                parentItem.IsExpanded = true;
                parentItem = parentItem.ParentItem;
            }
        }

2 Answers, 1 is accepted

Sort by
0
WILLIAM
Top achievements
Rank 1
answered on 12 Aug 2012, 04:20 PM
Is this not possible?  I think this would be common functionality.  I understand where you say that the items have to be bound before working with them.  But there is no event let inform the developer that these properties have been set or changed.  So how do you know when this is happened?  I've subscribed to a lot of the events, but the ItemsSource is not set yet.  Even the Loaded event is fired before this is set.

??
0
Petar Mladenov
Telerik team
answered on 13 Aug 2012, 12:17 PM
Hi William,

 Initially the RadTreeView generates / prepares only its root level RadTreeViewItems. Later when you expand a node, the RadTreeView generates the child items of the expanded item too. Expanding a particular RadTreeView item initially could be performed in various ways:
1) Using the ExpandAll() method - potentially very expensive method and should be used in very small trees.
2) Using the ExpandItemByPath method. It works similar to the GetItemByPath method described here. You give the path to the item and the tree will Generate the containers (RadtreeViewItems) on its way to the desired item. It might be expensive too because items you won't need to be expanded are being expanded too.
3) Binding the IsExpanded property of the RadTreeViewItem. This the most "MVVM"-like and the cheapest solution.
If you expose boolean property in your ViewModels you can bind it to the IsExpanded property  
of the RadTreeViewItems with ContainerBingins (Silverlight4 ) or StyleBindings (Silverlight 5). Suchapproach is demonstrated in this help article.

All the best,
Petar Mladenov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
TreeView
Asked by
WILLIAM
Top achievements
Rank 1
Answers by
WILLIAM
Top achievements
Rank 1
Petar Mladenov
Telerik team
Share this question
or