December 5th, 2007 by Kyle
Tags: ActionScript, ampersands, binding, expression, Flex, logical, logical-expressions, mxml, ternary, xml
Posted in: ActionScript, Flex
XML and ampersands do not mix well.
Flex uses mxml which is XML.
This can lead to issues when writing binding expressions in mxml tags for component properties when you are trying to write logical expressions using ‘and’.
Here is a simple sample that demonstrates how to escape the ampersands in the expression and how to create a logical expression that does not even use ‘and’, but achieves the same result.
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
[Bindable]
public var bOne:Boolean=false;
[Bindable]
public var bTwo:Boolean=false;
]]>
</mx:Script>
<mx:Button label="(click to toggle) bOne: {bOne}" click="bOne=!bOne"/>
<mx:Button label="(click to toggle) bTwo: {bTwo}" click="bTwo=!bTwo"/>
<mx:Label text="If bOne and bTwo are true, the buttons below will be Enabled"/>
<!–escape the ampersands for the logical and operator –>
<mx:Button label="enabled?" enabled="{(bOne && bTwo)}"/>
<!–or use this compounded logical statement which will evaluate to the same thing –>
<mx:Button label="enabled?" enabled="{(bOne ? bTwo : false)}"/>
</mx:Application>
Browse the source of this example.
Download a zipfile containing the source to this sample.
4 Comments »


Recent Comments