Mar 08
下面的例子展示了如何通过设置backgroundAlpha和backgroundColor风格,在Flex中设置DataGrid控件中字符背景以及控件背景颜色。
下面是具体代码:
Download: main.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <!-- http://blog.flexexamples.com/2008/03/06/setting-the-background-alpha-and-background-color-of-a-datagrid-control-in-flex/ -->
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
- layout="vertical"
- backgroundColor="white">
- <mx:Script>
- <![CDATA[
- private var defaultAlternatingItemColors:Array = [0xF7F7F7, 0xFFFFFF];
- private function checkBox_change(evt:Event):void {
- if (checkBox.selected) {
- dataGrid.setStyle("alternatingItemColors", defaultAlternatingItemColors);
- } else {
- dataGrid.setStyle("alternatingItemColors", []);
- }
- }
- ]]>
- </mx:Script>
- <mx:ArrayCollection id="arrColl">
- <mx:source>
- <mx:Array>
- <mx:Object c1="ColumnA.1" c2="ColumnB.1" />
- <mx:Object c1="ColumnA.2" c2="ColumnB.2" />
- <mx:Object c1="ColumnA.3" c2="ColumnB.3" />
- <mx:Object c1="ColumnA.4" c2="ColumnB.4" />
- <mx:Object c1="ColumnA.5" c2="ColumnB.5" />
- <mx:Object c1="ColumnA.6" c2="ColumnB.6" />
- <mx:Object c1="ColumnA.7" c2="ColumnB.7" />
- <mx:Object c1="ColumnA.8" c2="ColumnB.8" />
- <mx:Object c1="ColumnA.9" c2="ColumnB.9" />
- </mx:Array>
- </mx:source>
- </mx:ArrayCollection>
- <mx:ApplicationControlBar dock="true">
- <mx:Form styleName="plain">
- <mx:FormItem label="backgroundAlpha:">
- <mx:HSlider id="slider"
- minimum="0.0"
- maximum="1.0"
- value="1.0"
- liveDragging="true" />
- </mx:FormItem>
- <mx:FormItem label="backgroundColor:">
- <mx:ColorPicker id="colorPicker"
- selectedColor="purple" />
- </mx:FormItem>
- <mx:FormItem label="use alternatingItemColors:">
- <mx:CheckBox id="checkBox"
- label="[0xF7F7F7, 0xFFFFFF]"
- selected="true"
- change="checkBox_change(event);" />
- </mx:FormItem>
- </mx:Form>
- </mx:ApplicationControlBar>
- <mx:DataGrid id="dataGrid"
- dataProvider="{arrColl}"
- backgroundColor="{colorPicker.selectedColor}"
- backgroundAlpha="{slider.value}"
- width="300"
- rowCount="6"
- verticalScrollPolicy="on" />
- </mx:Application>
下面是执行效果,可以动手试试看:
你还可以在一个扩展的.CSS文件或者<mx:Style />中,通过下面的代码段来设置backgroundAlpha, backgroundColor, 和alternatingItemColors的风格:
- <mx:style>
- DataGrid {
- backgroundAlpha: 0.25;
- backgroundColor: #FF0000;
- alternatingItemColors: ClassReference(null);
- }
- </mx:style>
或者你可以通过像下面的的ActionScript设置backgroundAlpha,backgroundColor, 和alternatingItemColors来实现:
- <mx:script>
- <!--[CDATA[
- private function init():void {
- dataGrid.setStyle("backgroundAlpha", 0.25);
- dataGrid.setStyle("backgroundColor", 0xFF0000);
- dataGrid.setStyle("alternatingItemColors", null);
- }
- ]]-->
- </mx:script>
原文作者:Peter deHaan 翻译:minidxer
