Sometimes it is useful to have attributes which depend on the value of other attributes. That's precisely what bindings and expressions are for. They allow you to define attributes such that their values are a function of other attributes. And best of all, they automatically get updated if the values they refer to are changed. To declare an expression, the attribute must start with { and end with }.
You can refer to properties of the same element using the this keyword. The following example ensures that the height of the rectangle is the same as its width.
<Application>
<Shape>
<SolidFill color="0x333333">
<Rectangle width="100" height="{this.width}" />
</SolidFill>
</Shape>
</Application>
You can refer to properties of the root element using the root keyword. The root element of an application is its Application element. It has two useful properties, applicationHeight and applicationWidth, which respectively contain the width and the height of the application. The following example uses those properties to draw a rectangle that covers the entire area of the application.
<Application>
<Shape>
<SolidFill color="0x333333">
<Rectangle width="{root.applicationWidth}" height="{root.applicationHeight}" />
</SolidFill>
</Shape>
</Application>
Similarly, you can access properties of the parent element using the parent keyword. The following example produces the same results as the previous one.
<Application>
<Shape>
<SolidFill color="0x333333">
<Rectangle width="{parent.parent.parent.applicationWidth}" height="{parent.parent.parent.applicationHeight}" />
</SolidFill>
</Shape>
</Application>
You can access the properties of any element by using the getElementById() function of the root element. It requires one parameter, which is the ID of the element. You must first specify the id attribute of the element. The following example creates two rectangles, the second having the same dimensions as the first one.
<Application>
<Shape>
<SolidFill color="0x333333">
<Rectangle id="firstRectangle" width="100" height="100" />
<Rectangle x="150" width="{root.getElementById('firstRectangle').width}" height="{root.getElementById('firstRectangle').height}" />
</SolidFill>
</Shape>
</Application>
By using operators, you can create more complex bindings. The following example creates two rectangles, but the second is twice as big as the first one.
<Application>
<Shape>
<SolidFill color="0x333333">
<Rectangle id="firstRectangle" width="100" height="100" />
<Rectangle x="150" width="{root.getElementById('firstRectangle').width * 2}" height="{root.getElementById('firstRectangle').height * 2}" />
</SolidFill>
</Shape>
</Application>
You can use the Math object to use mathematical functions. The following example creates two rectangles with dimensions chosen randomly between 0.0 and 100.0.
<Application>
<Shape>
<SolidFill color="0x333333">
<Rectangle id="firstRectangle" width="{Math.random() * 100}" height="{Math.random() * 100}" />
<Rectangle x="150" width="{Math.random() * 100}" height="{Math.random() * 100}" />
</SolidFill>
</Shape>
</Application>
In this tutorial you've learned how to bind data using complex expressions. The benefits will become more apparent in later tutorials.
© 2008 by — Valid XHTML