The following example shows how you can set a header background image skin on the Halo DataGrid control (with default Spark skin) in Flex 4 by setting the headerBackgroundSkin style.

Full code after the jump.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see http://www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.
For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/07/02/setting-a-header-background-image-skin-on-a-halo-datagrid-control-in-flex-4 -->
<s:Application name="Halo_DataGrid_headerBackgroundSkin_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">
 
    <mx:DataGrid id="dataGrid"
            dataProvider="{Font.enumerateFonts(true)}"
            headerBackgroundSkin="skins.CustomDataGridHeaderBackgroundSkin"
            horizontalCenter="0" verticalCenter="0" />
 
</s:Application>

And the custom skin class, skins/CustomDataGridHeaderBackgroundSkin.mxml, is as follows

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/07/02/setting-a-header-background-image-skin-on-a-halo-datagrid-control-in-flex-4/ -->
<s:SparkSkin name="CustomDataGridHeaderBackgroundSkin"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        minWidth="21" minHeight="19">
 
    <!-- layer 1: fill -->
    <s:Rect left="0" right="0" top="0" bottom="0" alpha="0.6">
        <s:fill>
            <s:BitmapFill source="@Embed('assets/pattern_149.gif')" />
        </s:fill>
    </s:Rect>
 
    <!-- layer 2: fill lowlight -->
    <s:Rect left="0" right="0" bottom="0" height="9">
        <s:fill>
            <s:LinearGradient rotation="90">
                <s:GradientEntry color="black" alpha="0.0099" />
                <s:GradientEntry color="black" alpha="0.0627" />
            </s:LinearGradient>
        </s:fill>
    </s:Rect>
 
    <!-- layer 3: fill highlight -->
    <s:Rect left="0" right="0" top="0" height="9">
        <s:fill>
            <s:SolidColor color="white" alpha="0.33" />
        </s:fill>
    </s:Rect>
 
    <!-- layer 4: border - put on top of the fill so it doesn't disappear when scale is less than 1 -->
    <s:Rect left="0" right="0" bottom="0" height="1">
        <s:fill>
            <s:SolidColor color="#696969" />
        </s:fill>
    </s:Rect>
 
</s:SparkSkin>

View source is enabled in the following example.

Background image pattern copyright of Squidfingers.com.



You can also set the headerBackgroundSkin style in an external .CSS file or <Style/> block, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/07/02/setting-a-header-background-image-skin-on-a-halo-datagrid-control-in-flex-4/ -->
<s:Application name="Halo_DataGrid_headerBackgroundSkin_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/halo";
 
        mx|DataGrid {
            headerBackgroundSkin: ClassReference("skins.CustomDataGridHeaderBackgroundSkin");
        }
    </fx:Style>
 
    <mx:DataGrid id="dataGrid"
            dataProvider="{Font.enumerateFonts(true)}"
            horizontalCenter="0" verticalCenter="0" />
 
</s:Application>

Or, you can set the headerBackgroundSkin style using ActionSctipt, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/07/02/setting-a-header-background-image-skin-on-a-halo-datagrid-control-in-flex-4/ -->
<s:Application name="Halo_DataGrid_headerBackgroundSkin_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">
 
    <fx:Script>
        <![CDATA[
            import skins.CustomDataGridHeaderBackgroundSkin;
            import mx.skins.spark.DataGridHeaderBackgroundSkin;
 
            protected function btn_click(evt:MouseEvent):void {
                var skinClass:Class;
                if (btn.selected) {
                    // custom skin
                    skinClass = CustomDataGridHeaderBackgroundSkin;
                } else {
                    // default skin
                    skinClass = DataGridHeaderBackgroundSkin;
                }
                dataGrid.setStyle("headerBackgroundSkin", skinClass);
            }
        ]]>
    </fx:Script>
 
    <s:ToggleButton id="btn"
            label="Set header background skin"
            click="btn_click(event);"
            x="10" y="10" />
 
    <mx:DataGrid id="dataGrid"
            dataProvider="{Font.enumerateFonts(true)}"
            horizontalCenter="0" verticalCenter="0" />
 
</s:Application>

This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.

 
Tagged with:
 
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 Setting a header background image skin on a Halo DataGrid control in Flex 4

  1. Yohana says:

    Hi Peter deHaan, i have a big problem giving background color to header’s datagrid of sdk 4.5, i can’t find any properties to give it and i don’t know how can i provide color background to this headers. Please some suggestions?

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