代码:

 

   
   
   
   
  1. xml version="1.0" encoding="utf-8"?> 
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
  3.         layout="vertical" 
  4.         verticalAlign="middle" 
  5.         backgroundColor="white"> 
  6.  
  7.     <mx:Script> 
  8.          
  9.             private function openAllNodes():void {  
  10.                 tree.openItems = dp..node;  
  11.             }  
  12.  
  13.             private function closeAllNodes():void {  
  14.                 tree.openItems = [];  
  15.             }  
  16.         ]]> 
  17.     mx:Script> 
  18.  
  19.     <mx:XML id="dp"> 
  20.         <root> 
  21.             <node label="Parent 1"> 
  22.                 <node label="Child 1" /> 
  23.                 <node label="Child 2"> 
  24.                     <node label="Grandchild 1" /> 
  25.                     <node label="Grandchild 2" /> 
  26.                 node> 
  27.                 <node label="Child 3" /> 
  28.                 <node label="Child 4" /> 
  29.             node> 
  30.         root> 
  31.     mx:XML> 
  32.  
  33.     <mx:ApplicationControlBar dock="true"> 
  34.         <mx:Button label="Open all nodes" click="openAllNodes();" /> 
  35.         <mx:Button label="Close all nodes" click="closeAllNodes();" /> 
  36.     mx:ApplicationControlBar> 
  37.  
  38.     <mx:Tree id="tree" 
  39.             dataProvider="{dp}" 
  40.             showRoot="false" 
  41.             labelField="@label" 
  42.             width="200" /> 
  43.  
  44. mx:Application>