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

Expanding node when items not generated

2 Answers 33 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Julia
Top achievements
Rank 1
Julia asked on 02 Mar 2012, 04:43 PM
Hi all,

I am using a treeview bound to a hierarchy of ObservableCollections, and the bindings appear fine (including to a dependency property IsExpanded on the objects, and a pdependency property SelectedNode on the hierarchy as a whole) apart from one issue. My user can select a node and a control that fires this method:

public void NewConsolidation(object args)
        {
            SelectedNode.IsExpanded = true;
            var newNode = _hierarchy.AddNode(SelectedNode, "Another Child"); <-- inserts a node
            SelectedNode = newNode;
            SelectedNode.ParentNode.IsExpanded = true;
            SelectedNode.ParentNode.NodeName += "(has children)";
        }

With more than one node, collapsed, I see

+   Root Node <-- selected
 
go to

-  Root Node (has children)
     First Child
     Another Child <-- selected

but with only the root:

Root Node <-- selected 

goes to

+ Root Node (has children)

- that is, setting IsExpanded makes no difference if the node in question has never had children and been opened. I can see the binding is working, though, because the name change takes effect. I understand this is related to the treeview items not yet being generated - so how can I force them to be generated for this parent?

2 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 06 Mar 2012, 12:15 PM
Hello,

TreeView has support for a feature called LoadOnDemand (kind of adding items dynamically). You could take a look at the following article for more info: http://www.telerik.com/help/silverlight/radtreeview-features-load-on-demand.html .

Going back to your case. You could try to delay the call to IsExpanded property a bit, like follows:

public void NewConsolidation(object args)
{
    //SelectedNode.IsExpanded = true;//No need to call it before new item is added.
    var newNode = _hierarchy.AddNode(SelectedNode, "Another Child"); <-- inserts a node
    SelectedNode = newNode;
    SelectedNode.ParentNode.NodeName += "(has children)";
    this.Dispatcher.BeginInvoke(new Action(() => { SelectedNode.ParentNode.IsExpanded = true; }));
}

Also make sure that:
1) Newly added items is correctly inserted in the Items collection of the visual element
2) Do not call IsExpanded=true before you add the new element.

Hope this helps.Kind regards,
Hristo
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Julia
Top achievements
Rank 1
answered on 07 Mar 2012, 10:11 AM
That does exactly what I'm after - thanks very much!

Best wishes,
Julia
Tags
TreeView
Asked by
Julia
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Julia
Top achievements
Rank 1
Share this question
or