2011-11-05 79 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將在數據提供者更改時將選定索引設置爲指定值。

相關問題