Flex中设置DataGrid控件中字符背景以及控件背景颜色

下面的例子展示了如何通过设置 backgroundAlphabackgroundColor风格,在Flex中设置DataGrid控件中字符背景以及控件背景颜色。
下面是具体代码:
Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- http://blog.flexexamples.com/2008/03/06/setting-the-background-alpha-and-background-color-of-a-datagrid-control-in-flex/ -->
  3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  4.         layout="vertical"
  5.         backgroundColor="white">
  6.     <mx:Script>
  7.         <![CDATA[
  8.             private var defaultAlternatingItemColors:Array = [0xF7F7F7, 0xFFFFFF];
  9.             private function checkBox_change(evt:Event):void {
  10.                 if (checkBox.selected) {
  11.                     dataGrid.setStyle("alternatingItemColors", defaultAlternatingItemColors);
  12.                 } else {
  13.                     dataGrid.setStyle("alternatingItemColors", []);
  14.                 }
  15.             }
  16.         ]]>
  17.     </mx:Script>
  18.     <mx:ArrayCollection id="arrColl">
  19.         <mx:source>
  20.             <mx:Array>
  21.                 <mx:Object c1="ColumnA.1" c2="ColumnB.1" />
  22.                 <mx:Object c1="ColumnA.2" c2="ColumnB.2" />
  23.                 <mx:Object c1="ColumnA.3" c2="ColumnB.3" />
  24.                 <mx:Object c1="ColumnA.4" c2="ColumnB.4" />
  25.                 <mx:Object c1="ColumnA.5" c2="ColumnB.5" />
  26.                 <mx:Object c1="ColumnA.6" c2="ColumnB.6" />
  27.                 <mx:Object c1="ColumnA.7" c2="ColumnB.7" />
  28.                 <mx:Object c1="ColumnA.8" c2="ColumnB.8" />
  29.                 <mx:Object c1="ColumnA.9" c2="ColumnB.9" />
  30.             </mx:Array>
  31.         </mx:source>
  32.     </mx:ArrayCollection>
  33.     <mx:ApplicationControlBar dock="true">
  34.         <mx:Form styleName="plain">
  35.             <mx:FormItem label="backgroundAlpha:">
  36.                 <mx:HSlider id="slider"
  37.                         minimum="0.0"
  38.                         maximum="1.0"
  39.                         value="1.0"
  40.                         liveDragging="true" />
  41.             </mx:FormItem>
  42.             <mx:FormItem label="backgroundColor:">
  43.                 <mx:ColorPicker id="colorPicker"
  44.                         selectedColor="purple" />
  45.             </mx:FormItem>
  46.             <mx:FormItem label="use alternatingItemColors:">
  47.                 <mx:CheckBox id="checkBox"
  48.                         label="[0xF7F7F7, 0xFFFFFF]"
  49.                         selected="true"
  50.                         change="checkBox_change(event);" />
  51.             </mx:FormItem>
  52.         </mx:Form>
  53.     </mx:ApplicationControlBar>
  54.     <mx:DataGrid id="dataGrid"
  55.             dataProvider="{arrColl}"
  56.             backgroundColor="{colorPicker.selectedColor}"
  57.             backgroundAlpha="{slider.value}"
  58.             width="300"
  59.             rowCount="6"
  60.             verticalScrollPolicy="on" />
  61. </mx:Application>
下面是执行效果,可以动手试试看:
你还可以在一个扩展的.CSS文件或者<mx:Style />中,通过下面的代码段来设置 backgroundAlpha, backgroundColor, 和 alternatingItemColors的风格
  1. <mx:style>
  2. DataGrid {
  3. backgroundAlpha: 0.25;
  4. backgroundColor: #FF0000;
  5. alternatingItemColors: ClassReference(null);
  6. }
  7. </mx:style>
或者你可以通过像下面的的ActionScript设置 backgroundAlpha, backgroundColor, 和 alternatingItemColors来实现:
  1. <mx:script>
  2. <!--[CDATA[
  3. private function init():void {
  4. dataGrid.setStyle("backgroundAlpha", 0.25);
  5. dataGrid.setStyle("backgroundColor", 0xFF0000);
  6. dataGrid.setStyle("alternatingItemColors", null);
  7. }
  8. ]]-->
  9. </mx:script>

你可能感兴趣的:(职场,休闲)