2011-06-10 69 views

回答

3

退房my answerthis question

我会在定义ItemRenderer做到这一点,并通过覆盖set dataupdateDisplayList功能设置字体颜色。

this article

应用:

<?xml version="1.0" encoding="utf-8"?> 
<!-- http://blog.flexexamples.com/2007/08/20/formatting-a-flex-datagrid-control-using-a-custom-item-renderer/ --> 
<mx:Application name="DataGridColumn_itemRenderer_test " 
     xmlns:mx="http://www.adobe.com/2006/mxml" 
     layout="vertical" 
     verticalAlign="middle" 
     backgroundColor="white"> 

    <mx:Script> 
     <![CDATA[ 
      import mx.controls.dataGridClasses.DataGridColumn; 
      import mx.utils.ObjectUtil; 

      private function price_labelFunc(item:Object, column:DataGridColumn):String { 
       return currencyFormatter.format([email protected]); 
      } 

      private function price_sortCompareFunc(itemA:Object, itemB:Object):int { 
       return ObjectUtil.numericCompare([email protected], [email protected]); 
      } 
     ]]> 
    </mx:Script> 

    <mx:XML id="itemsXML"> 
     <items> 
      <item name="Item 1" price="1.32" /> 
      <item name="Item 2" price="-12.23" /> 
      <item name="Item 3" price="4.96" /> 
      <item name="Item 4" price="-0.94" /> 
     </items> 
    </mx:XML> 

    <mx:Style> 
     .centered { 
      text-align: center; 
     } 
    </mx:Style> 

    <mx:CurrencyFormatter id="currencyFormatter" 
      precision="2" 
      useNegativeSign="false" /> 

    <mx:DataGrid id="dataGrid" dataProvider="{itemsXML.item}"> 
     <mx:columns> 
      <mx:DataGridColumn dataField="@name" 
        headerText="Name:" 
        headerStyleName="centered" /> 

      <mx:DataGridColumn dataField="@price" 
        headerText="Price:" 
        textAlign="right" 
        headerStyleName="centered" 
        labelFunction="price_labelFunc" 
        sortCompareFunction="price_sortCompareFunc" 
        itemRenderer="PriceLabel" /> 
     </mx:columns> 
    </mx:DataGrid> 

</mx:Application> 

PriceLabel.as:

/** http://blog.flexexamples.com/2007/08/20/formatting-a-flex-datagrid-control-using-a-custom-item-renderer/ */ 
package { 
    import mx.controls.Label; 
    import mx.controls.listClasses.*; 

    public class PriceLabel extends Label { 

     private const POSITIVE_COLOR:uint = 0x000000; // Black 
     private const NEGATIVE_COLOR:uint = 0xFF0000; // Red 

     override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void { 
      super.updateDisplayList(unscaledWidth, unscaledHeight); 

      /* Set the font color based on the item price. */ 
      setStyle("color", (parseFloat([email protected]) <= 0) ? NEGATIVE_COLOR : POSITIVE_COLOR); 
     } 
    } 
} 
+0

+1嘿,很好的答案。虽然对于itemRenderer,虽然对于itemRenderer会使用一个.mxml,但是对于某些人来说更容易理解 – Ryan 2011-06-10 15:50:26

+0

@Brian True,它可能会更容易些,但是我认为你已经提到了itemRenderer,所以我会建议略有不同的方法。 :) – 2011-06-10 16:11:56

2

我会建议你使用的itemRenderer用于DataGrid和使用的itemRenderer的“数据”变量,检查值是什么例如> 20。然后,基于此设置itemRenderer的背景颜色。

如果您不知道如何使用itemRenderer,请执行谷歌搜索。那里有大量的DataGrid itemRenderer示例。

相关问题