Setting the base color on the nested scrollbars in the MX DataGrid control in Flex 4

The following example shows how you can change the base color on the MX DataGrid control’s horizontal and vertical scroll bars by setting the horizontalScrollBarStyleName and verticalScrollBarStyleName styles to a custom style containing the chromeColor style.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see 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/09/27/setting-the-base-color-on-the-nested-scrollbars-in-the-halo-datagrid-control-in-flex-4/ -->
<s:Application name="MX_DataGrid_verticalScrollBarStyleName_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
 
    <fx:Style>
        .scrollBarStyles {
            chromeColor: haloBlue;
        }
    </fx:Style>
 
    <mx:DataGrid id="dataGrid"
            horizontalScrollPolicy="on"
            verticalScrollPolicy="on"
            horizontalScrollBarStyleName="scrollBarStyles"
            verticalScrollBarStyleName="scrollBarStyles"
            rowCount="5"
            width="150"
            horizontalCenter="0" verticalCenter="0">
        <mx:dataProvider>
            <mx:ArrayList>
                <fx:Object c1="1.One" c2="1.Two" c3="1.Three" />
                <fx:Object c1="2.One" c2="2.Two" c3="2.Three" />
                <fx:Object c1="3.One" c2="3.Two" c3="3.Three" />
                <fx:Object c1="4.One" c2="4.Two" c3="4.Three" />
                <fx:Object c1="5.One" c2="5.Two" c3="5.Three" />
                <fx:Object c1="6.One" c2="6.Two" c3="6.Three" />
                <fx:Object c1="7.One" c2="7.Two" c3="7.Three" />
                <fx:Object c1="8.One" c2="8.Two" c3="8.Three" />
                <fx:Object c1="9.One" c2="9.Two" c3="9.Three" />
            </mx:ArrayList>
        </mx:dataProvider>
    </mx:DataGrid>
 
</s:Application>

[GoogleAdsWide]

View source is enabled in the following example.

You can also set the horizontalScrollBarStyleName and verticalScrollBarStyleName styles 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/09/27/setting-the-base-color-on-the-nested-scrollbars-in-the-halo-datagrid-control-in-flex-4/ -->
<s:Application name="MX_DataGrid_verticalScrollBarStyleName_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/mx";
 
        .scrollBarStyles {
            chromeColor: haloBlue;
        }
 
        mx|DataGrid {
            horizontalScrollBarStyleName: scrollBarStyles;
            verticalScrollBarStyleName: scrollBarStyles;
        }
    </fx:Style>
 
    <mx:DataGrid id="dataGrid"
            horizontalScrollPolicy="on"
            verticalScrollPolicy="on"
            rowCount="5"
            width="150"
            horizontalCenter="0" verticalCenter="0">
        <mx:dataProvider>
            <mx:ArrayList>
                <fx:Object c1="1.One" c2="1.Two" c3="1.Three" />
                <fx:Object c1="2.One" c2="2.Two" c3="2.Three" />
                <fx:Object c1="3.One" c2="3.Two" c3="3.Three" />
                <fx:Object c1="4.One" c2="4.Two" c3="4.Three" />
                <fx:Object c1="5.One" c2="5.Two" c3="5.Three" />
                <fx:Object c1="6.One" c2="6.Two" c3="6.Three" />
                <fx:Object c1="7.One" c2="7.Two" c3="7.Three" />
                <fx:Object c1="8.One" c2="8.Two" c3="8.Three" />
                <fx:Object c1="9.One" c2="9.Two" c3="9.Three" />
            </mx:ArrayList>
        </mx:dataProvider>
    </mx:DataGrid>
 
</s:Application>

You can also set the horizontalScrollBarStyleName and verticalScrollBarStyleName styles using ActionScript, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/09/27/setting-the-base-color-on-the-nested-scrollbars-in-the-halo-datagrid-control-in-flex-4/ -->
<s:Application name="MX_DataGrid_verticalScrollBarStyleName_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:controlBarContent>
        <s:Button id="btn"
                label="Set scroll bar style names"
                click="btn_click(event);" />
    </s:controlBarContent>
 
    <fx:Style>
        .scrollBarStyles {
            chromeColor: haloBlue;
        }
    </fx:Style>
 
    <fx:Script>
        <![CDATA[
            protected function btn_click(evt:MouseEvent):void {
                dataGrid.setStyle("horizontalScrollBarStyleName", "scrollBarStyles");
                dataGrid.setStyle("verticalScrollBarStyleName", "scrollBarStyles");
            }
        ]]>
    </fx:Script>
 
    <mx:DataGrid id="dataGrid"
            horizontalScrollPolicy="on"
            verticalScrollPolicy="on"
            rowCount="5"
            width="150"
            horizontalCenter="0" verticalCenter="0">
        <mx:dataProvider>
            <mx:ArrayList>
                <fx:Object c1="1.One" c2="1.Two" c3="1.Three" />
                <fx:Object c1="2.One" c2="2.Two" c3="2.Three" />
                <fx:Object c1="3.One" c2="3.Two" c3="3.Three" />
                <fx:Object c1="4.One" c2="4.Two" c3="4.Three" />
                <fx:Object c1="5.One" c2="5.Two" c3="5.Three" />
                <fx:Object c1="6.One" c2="6.Two" c3="6.Three" />
                <fx:Object c1="7.One" c2="7.Two" c3="7.Three" />
                <fx:Object c1="8.One" c2="8.Two" c3="8.Three" />
                <fx:Object c1="9.One" c2="9.Two" c3="9.Three" />
            </mx:ArrayList>
        </mx:dataProvider>
    </mx:DataGrid>
 
</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.