actionscript-3 - 当我将子节点添加到空的展开节点时,树不刷新

标签 actionscript-3 actionscript air flex4

我将 mx:Tree 绑定(bind)到我的 Parent 类的对象。 Parent 有 ArrayCollection 的 child 。当我展开一个空节点并添加一些子节点时,它不会刷新,直到我折叠并展开该节点。如果节点已经有子节点,一切正常,新节点立即出现。我该如何解决?

    <?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;
            }
        }
    }

最佳答案

您可以使用 TreeinvalidateList() 方法告诉树在下一个生命周期更新时刷新自身:

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

关于actionscript-3 - 当我将子节点添加到空的展开节点时,树不刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17279795/

相关文章:

flash - 如何缩放填充而不是Actionscript中的笔划?

apache-flex - 如何将XPath与AS3结合使用,并获得仍引用到XMLDocument对象的结果?

android - Flex/Actionscript拖放/滑动效果(模拟android/ios界面)

ios - 优化技巧? AS3 iOS AIR 3.0

android - 从 Adob​​e Air android 应用程序发布到 facebook 墙

javascript - 从 javascript/jquery 调用 AS 函数

javascript - 光标 : crosshair; for buttonImg

actionscript-3 - FLEX 4 s :Scroller, 如何将包含的组件纳入 View ?

javascript - AS3 -ExternalInterface.addCallback,访问未定义的属性

apache-flex - 使用byteArray和Adobe Air或Flex录制x秒的静音音频文件