2013-06-24 24 views
3

我將mx:Tree綁定到我的父類的對象。父項有子項的ArrayCollection。當我展開一個空節點並添加一些子節點時,它將不刷新,直到我摺疊並展開此節點。如果節點已經有孩子,一切正常,新節點立即出現。我該如何解決它?當我將子項添加到空的擴展節點時樹不刷新

<?xml version="1.0" encoding="utf-8"?> 
    <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
          xmlns:s="library://ns.adobe.com/flex/spark" 
          xmlns:mx="library://ns.adobe.com/flex/mx"> 

     <fx:Script> 
      <![CDATA[ 
    import mx.collections.ArrayCollection; 
       [Bindable] 
       public var selectedNode:Parent; 

       [Bindable] 
       public var treeData:ArrayCollection = new ArrayCollection(); 

       public function treeChanged(evt:Event):void { 
        selectedNode = Tree(evt.target).selectedItem as Parent; 
       } 

       public function btnClick():void 
       { 
        if (selectedNode) 
        { 
         (selectedNode as Parent).children.addItem(new Parent()); 
        } 
        else 
        { 
         treeData.addItem(new Parent()); 
        } 

       } 
      ]]> 
     </fx:Script> 


     <s:Panel title="Halo Tree Control Example" 
       width="75%" height="75%" 
       horizontalCenter="0" verticalCenter="0"> 
      <s:VGroup left="10" right="10" top="10" bottom="10"> 
       <mx:Button click="{btnClick()}" label="Add"></mx:Button> 

       <mx:HDividedBox width="100%" height="100%"> 
        <mx:Tree id="myTree" width="50%" height="100%" labelField="@label" 
          showRoot="false" dataProvider="{treeData}" change="treeChanged(event);"/> 
        <s:TextArea height="100%" width="50%" 
          text="Selected Item: {selectedNode}"/> 
       </mx:HDividedBox> 
      </s:VGroup> 

     </s:Panel> 
    </s:WindowedApplication> 

父類:

package 
    { 
     import mx.collections.ArrayCollection; 
     public class Parent 
     { 
      private var _children:ArrayCollection = new ArrayCollection(); 

      public function Parent() 
      { 

      } 

      [Bindable] 
      public function get children():ArrayCollection 
      { 
       return _children; 
      } 

      public function set children(value:ArrayCollection):void 
      { 
       _children = value; 
      } 
     } 
    } 

回答

3

您可以使用TreeinvalidateList()方法通知樹刷新自己的下一個生命週期更新:

public function btnClick():void 
{ 
    if (selectedNode) 
    { 
     (selectedNode as Parent).children.addItem(new Parent()); 
     myTree.invalidateList(); 
    } 
    else 
    { 
     treeData.addItem(new Parent()); 
    } 
} 
相關問題