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.
Tweet
5 Comments »

May 22nd, 2008 at 10:08 pm
Thanks – this is just what I was looking for!
But how does one handle the use of either (less than or greater than) in an inline expressions, like those with the damn && ?
May 22nd, 2008 at 10:11 pm
I guess following your lead use < or >
May 22nd, 2008 at 10:13 pm
wow the symbols replaced the ampersand + lt + semicolon code to give the less than sign in my last comment.
July 8th, 2008 at 4:51 pm
Arnold was trying to point out that you should write < to say <, and > to say >
September 4th, 2008 at 1:02 am
Thanks, it’s quite painful to do logic operator in mxml.