2011-11-05 81 views
0

我有一个Spark ButtonBar,并且正确地连接了ViewStack。目前,当我运行应用程序(AIR)时,ButtonBar中的第一个按钮被默认选中。我怎样才能使第二个按钮被默认选中?如何在ButtonBar中默认选中一个按钮?

<mx:ViewStack id="viewStack"> 
    <s:NavigatorContent id="Page 1"> 
     <!-- Other stuff in here --> 
    </s:NavigatorContent> 
    <s:NavigatorContent id="Page 2"> 
     <!-- Other stuff in here --> 
    </s:NavigatorContent> 
</mx:ViewStack> 

<s:ButtonBar dataProvider="{viewStack}" selectedIndex="1"></s:ButtonBar> 
+0

以前没有用过,但按钮栏没有selectedIndex?它有,如果我记得在框架3.0与flex建设者3 –

+0

我试过了,但没有第一个ButtonBar按钮保持选中,无论我为所选的索引放置的值。奇怪的! – Titus

+0

呵呵,试着把它分配给它的数据提供者后,或者可能在creationcomplete事件中,只是为了避免技术问题..你尝试点击一个按钮,并在那一刻改变selectedIndex像myButtonBar.selectedIndex = 1 ;如果它的工作..是selectedIndex正在分配之前,你的数据提供者被分配.. ..你需要分配它之后,不knwo如果它有dataproviderChanged事件..或类似的东西 –

回答

1

我做到了这一点,它运作良好。你确定selectedIndex不起作用吗?

<?xml version="1.0" encoding="utf-8"?> 
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
       xmlns:s="library://ns.adobe.com/flex/spark" 
       xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> 
    <fx:Declarations> 
     <!-- Place non-visual elements (e.g., services, value objects) here --> 
    </fx:Declarations> 
    <s:ButtonBar 
     selectedIndex="2" width="400" height="300"> 
     <s:dataProvider> 
      <s:ArrayCollection> 
       <fx:String>1</fx:String> 
       <fx:String>2</fx:String> 
       <fx:String>3</fx:String> 
      </s:ArrayCollection> 
     </s:dataProvider> 

    </s:ButtonBar> 

</s:Application> 

编辑:

这可以帮助您?

<?xml version="1.0" encoding="utf-8"?> 
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
       xmlns:s="library://ns.adobe.com/flex/spark" 
       xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="init()"> 
    <fx:Declarations> 
     <!-- Place non-visual elements (e.g., services, value objects) here --> 
    </fx:Declarations> 
    <fx:Script> 
     <![CDATA[ 
      import mx.collections.ArrayCollection; 
      import mx.events.CollectionEvent; 
      [Bindable] 
      private var dataSource:ArrayCollection = new ArrayCollection(); 

      private function init():void 
      { 

       dataSource = new ArrayCollection(new Array("1","2","3")); 
       dataSource.addEventListener(CollectionEvent.COLLECTION_CHANGE, collectionEventChange); 
       dataSource.dispatchEvent(new CollectionEvent(CollectionEvent.COLLECTION_CHANGE)); 

      } 

      private function collectionEventChange(event:CollectionEvent):void 
      { 
       this.btnBar.selectedIndex = 2; 
      } 
     ]]> 
    </fx:Script> 
    <s:ButtonBar id="btnBar" dataProvider="{dataSource}" 
     width="400" height="300" > 

    </s:ButtonBar> 

</s:Application> 
+0

感谢您的答案。这就是我做的: '' 我也试过: ''但'selectedIndex'似乎仍然不起作用。 – Titus

+0

你什么时候分配数据源? –

+0

我在里面用''来使用''(我更新了我的问题以反映这一点。)再次感谢您的帮助! – Titus

0

您需要设置已被填充按钮栏后所选择的指数后的组件已初始化和。按钮栏只会在检测到数据提供者发生变化时才创建按钮(通过在绑定变量的同时附加一个变更监视器)。

这就是为什么先生的修改后的答案具有在CollectionEvent侦听器中设置所选索引的内容。

更强大的解决方案是对buttonbar进行子类化并调整它,添加一个defaultSelectedIndex成员或创建一个可以将此功能添加到listbase的任何子类的mixin。 defaultSelectedIndex将在数据提供者更改时将选定索引设置为指定值。

相关问题