隐藏ButtonBar中的按钮

时间:2013-08-27 21:51:47

标签: actionscript-3 flex

我想知道是否有任何方法可以隐藏ButtonBar中的特定按钮。根据这个答案(以及第二个答案中提供的链接)Disable individual buttons in a buttonbar我需要使用ButtonBar的getChildAt方法,但是当我这样做时,我得到自定义皮肤对象而不是Button对象。我想知道如何访问Button对象。

谢谢!

1 个答案:

答案 0 :(得分:2)

假设按钮栏中的所有按钮都会同时渲染,而您不需要滚动条......

使用Spark ButtonBar,您可以直接访问外观部件以访问按钮。概念上是这样的:

var button : Button = mySparkButtonBarInstance.dataGroup.getElementAt(SomeIndex);
button.visible = false; // or true
button.includeInLayout = false;  // or true

如果您的ButtonBar可以使用虚拟布局并需要滚动,那么这将不起作用。


编辑:以下是演示此技术的工作代码:

<?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.core.IVisualElement;
            protected function button1_clickHandler(event:MouseEvent):void
            {
                trace(buttonBar.dataGroup.getElementAt(0));
                var button :IVisualElement = buttonBar.dataGroup.getElementAt(0);
                button.visible = false; // or true
                button.includeInLayout = false;  // or true         }
            }
        ]]>
    </fx:Script>

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <s:layout>
        <s:VerticalLayout paddingLeft="20" paddingTop="20"/>
    </s:layout>

    <s:ButtonBar id="buttonBar">  
        <mx:ArrayCollection>
            <fx:String>Flash</fx:String> 
            <fx:String>Director</fx:String> 
            <fx:String>Dreamweaver</fx:String> 
            <fx:String>ColdFusion</fx:String> 
        </mx:ArrayCollection>
    </s:ButtonBar>

    <s:Button label="Remove" click="button1_clickHandler(event)" />

</s:WindowedApplication>