views:

198

answers:

1

I'm trying to databind an object's property to a ComboBox's (editable=true) text property. This property is of type Number.

If I bind using the inline syntax, it works:

<mx:ComboBox text="{myObj.prop}">

If I bind using mx:Binding, I receive an error:

<mx:Binding source="{myObj.prop}" destination="combobox.text" />

// 1067: Implicit coercion of a value of type Number to an unrelated type String.

Why this difference in behaviour?

Property definition:

private var _prop: Number;

[Bindable] public function get prop(): Number { return _prop; }
public function set prop(value: Number): void { _prop = value; }
A: 

Initially I thought: The mx:Binding source should be the field name itself, not the value. Flex is complaining because it is dereferencing myObj.prop because of the {} and seeing the value there (a Number) when it wants a string with the field name.

<mx:Binding source="myObj.prop" destination="combobox.text" />

However:

ActionScript inside curly braces is allowed in the mx:Binding source expression, and is required in this case. See Adobe's data binding examples.

The text property is expecting a String to be assigned to it, so you will want to cast in your binding:

<mx:Binding source="{String(myObj.prop)}" destination="combobox.text" />

My apologies for the initial misleading answer, hopefully this is on the right track.

Michael Greene
I still receive the same error after removing the brackets.
Dark Falcon
It would be helpful to know the definition of `myObj.prop`.
Michael Greene
Perfect. The inverse binding also works, using parseInt.
Dark Falcon