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>
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

One Response to Changing a VBox container’s background image attachment in Flex

  1. Niran says:

    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

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree