<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/11/28/detecting-when-a-user-opens-or-closes-a-tree-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="horizontal"
        verticalAlign="middle"
        backgroundColor="white" viewSourceURL="srcview/index.html">

    <mx:Script>
        <![CDATA[
            import mx.events.CollectionEvent;
            import mx.events.CollectionEventKind;
            import mx.events.TreeEvent;

            private function tree_itemOpening(evt:TreeEvent):void {
                arrColl.addItem({type:evt.type, label:evt.item.@label, opening:evt.opening});
            }

            private function arrColl_collectionChange(evt:CollectionEvent):void {
                switch (evt.kind) {
                    case CollectionEventKind.ADD:
                        callLater(autoScroll);
                        break;
                }
            }

            private function autoScroll():void {
                dataGrid.verticalScrollPosition = dataGrid.maxVerticalScrollPosition;
            }
        ]]>
    </mx:Script>

    <mx:ArrayCollection id="arrColl"
            collectionChange="arrColl_collectionChange(event);" />

    <mx:XMLListCollection id="xmlListColl">
        <mx:source>
            <mx:XMLList>
                <node label="One">
                    <node label="One.1">
                        <node label="One.1.1">
                            <node label="One.1.1.1">
                                <node label="One.1.1.1.1" />
                            </node>
                        </node>
                    </node>
                    <node label="One.2" />
                    <node label="One.3" />
                </node>
                <node label="Two">
                    <node label="Two.1" />
                    <node label="Two.2" />
                    <node label="Two.3" />
                </node>
            </mx:XMLList>
        </mx:source>
    </mx:XMLListCollection>

    <mx:Tree id="tree"
            dataProvider="{xmlListColl}"
            labelField="@label"
            width="250"
            rowCount="6"
            showScrollTips="true"
            itemOpening="tree_itemOpening(event);" />

    <mx:DataGrid id="dataGrid"
            dataProvider="{arrColl}"
            verticalScrollPolicy="on"
            width="100%"
            height="{tree.height}">
        <mx:columns>
            <mx:DataGridColumn dataField="type" />
            <mx:DataGridColumn dataField="label" />
            <mx:DataGridColumn dataField="opening" />
        </mx:columns>
    </mx:DataGrid>

</mx:Application>
