Expanding node when items not generated

3 posts, 0 answers
  1. Julia
    Julia avatar
    2 posts
    Member since:
    Nov 2011

    Posted 02 Mar 2012 Link to this post

    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. Hristo
    Admin
    Hristo avatar
    352 posts

    Posted 06 Mar 2012 Link to this post

    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 >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Julia
    Julia avatar
    2 posts
    Member since:
    Nov 2011

    Posted 07 Mar 2012 Link to this post

    That does exactly what I'm after - thanks very much!

    Best wishes,
    Julia
Back to Top