05
Jan
08

Changing a VBox container’s background image attachment in Flex

The following example shows how you can set a background image and change its attachment to scrolling or fixed for a Flex VBox container by setting the backgroundImage and backgroundAttachment styles.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/01/05/changing-a-vbox-containers-background-image-attachment-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Style>
        .containerVBox {
            backgroundColor: white;
            borderThickness: 1;
            borderColor: black;
            borderStyle: solid;
            paddingLeft: 5;
            paddingRight: 5;
            paddingTop: 5;
            paddingBottom: 5;
        }
    </mx:Style>

    <mx:String id="lorem" source="lorem.txt" />

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="backgroundAttachment:">
                <mx:ComboBox id="comboBox">
                    <mx:dataProvider>
                        <mx:Array>
                            <mx:Object label="scroll" />
                            <mx:Object label="fixed" />
                        </mx:Array>
                    </mx:dataProvider>
                </mx:ComboBox>
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:VBox styleName="containerVBox">
        <mx:VBox id="vBox"
                backgroundImage="@Embed('Fx.png')"
                backgroundAttachment="{comboBox.selectedItem.label}"
                width="500"
                height="250">
            <mx:Text width="100%" text="{lorem}" />
        </mx:VBox>
    </mx:VBox>

</mx:Application>

View source is enabled in the following example.

If you want to set the backgroundImage and backgroundAttachment styles using an external .CSS file or <mx:Style /> block, you can use code similar to the following snippet:

<mx:Style>
    VBox {
        backgroundImage: Embed("Fx.png");
        backgroundAttachment: fixed;
    }
</mx:Style>

Or, if you want to set the backgroundImage and backgroundAttachment styles using ActionScript, you can use code similar to the following snippet:

<mx:Script>
    <![CDATA[
        private function vBox_initialize():void {
            /* Dynamically load image at runtime. */
            vBox.setStyle("backgroundImage", "Fx.png");
            vBox.setStyle("backgroundAttachment", "fixed");
        }
    ]]>
</mx:Script>

1 Response to “Changing a VBox container's background image attachment in Flex”


  1. 1 Niran Apr 3rd, 2008 at 10:02 pm

    How do I change the justification of the background image? In other words, if I want the background image to be left justified how is that accomplished?

    Thanks
    Niran

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".