2011-08-03 17 views
0

我有一個自定義組件,其中有一個advancedDataGrid。我希望這個組件是可重用的,因此需要將datagid selectionMode設置爲組件屬性。自定義組件dataGrid selectionMode作爲屬性

在MXML我想設置屬性是這樣的:

<comp:MyComp itemDataGridSelectionMode="singleCell" .../> 

內MyComp動作我有這樣的元標籤:

[Inspectable(enumeration="singleRow, multipleRows, singleCell, multipleCells", defaultValue="singleRow")] 
public var itemDataGridSelectionMode:String; 

如何綁定這個itemDataGridSelectionMode變量advancedDatagrid的SelectionMode?

更新:這裏是一個小的測試應用程序完全正常工作代碼:

<!--MyComp.mxml--> 
<?xml version="1.0" encoding="utf-8"?> 
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" width="638" height="500"> 
<fx:Declarations> 
    <!-- Place non-visual elements (e.g., services, value objects) here --> 
</fx:Declarations> 

<fx:Script> 
    <![CDATA[ 
     [Inspectable(enumeration="singleRow, multipleRows, singleCell, multipleCells", defaultValue="singleRow")] 
     public function set itemsSelectionMode(value:String):void 
     { 
      this.adgItems.selectionMode = value; 
     } 

     public function get itemsSelectionMode():String 
     { 
      return this.adgItems.selectionMode; 
     } 
    ]]> 
</fx:Script> 
<mx:AdvancedDataGrid id="adgItems" designViewDataType="flat" width="100%" height="100%"> 
    <mx:columns> 
     <mx:AdvancedDataGridColumn headerText="Column 1" dataField="col1"/> 
     <mx:AdvancedDataGridColumn headerText="Column 2" dataField="col2"/> 
     <mx:AdvancedDataGridColumn headerText="Column 3" dataField="col3"/> 
    </mx:columns> 
</mx:AdvancedDataGrid> 
</s:Group> 

<!-- Application.mxml --> 
<?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" xmlns:comp="*"> 
<fx:Declarations> 
    <!-- Place non-visual elements (e.g., services, value objects) here --> 
</fx:Declarations> 
<comp:MyComponent x="272" y="86" itemsSelectionMode="singleCell"/> 
</s:Application> 

錯誤:無效的值:multipleRows。它必須是singleRow,multipleRows,singleCell,multipleCells之一。

回答

2

其中有您的自定義組件內的公共變種,這樣做:

[Inspectable(enumeration="singleRow, multipleRows, singleCell, multipleCells", defaultValue="singleRow")] 
public function set itemDataGridSelectionMode(value:String):void 
{ 
    advancedDatagrid.selectionMode = value; 
} 

public function get itemDataGridSelectionMode():String 
{ 
    return advancedDatagrid.selectionMode; 
} 
+0

我認爲這是正確的方法。我會試試看。 –

+0

它仍然給出同樣的錯誤。它需要單行而不是其他。 –

+0

什麼是錯誤?你嘗試過調試嗎? –

0

我想你可以設置itemDataGridSelectionMode爲[Bindable],然後可以綁定它與AdvancedDataGrid的selectionMode屬性。

+0

我試過了,並給出了一個錯誤: 值無效:multipleRows。它必須是singleRow,multipleRows,singleCell,multipleCells之一。 –

+0

如果是這種情況,請嘗試按照@J_A_X建議使用setter –

0

一種方法是:

BindingUtils.bindProperty(datagridId, 'selectionMode', this, itemDataGridSelectionMode); 

或使用setter方法,而不是變量定義。

+1

以前的解決方案是一個可怕的解決方案,因爲您在一個不需要的地方使用綁定。後者是唯一真正的答案。 –