Styling foreground and background colors in the Flex / datagrid list

Flex provides the CSS property "selectionColor" to style the background color of the selected list / datagrid. However, I cannot figure out how to style the foreground or text of the selected list. It looks like you can only change the foreground color for all lines.

So, for example, I need a very dark color for the selection background and a very light color for the selected background. Similarly, you want to get a light color for text to highlight and a dark color for text to deselect.

I know that I can do this with a special element renderer, but that seems pretty dumb. The point is to style all the lists / datagrids in my application. I don’t want to set up custom rendering of elements or extend the Datagrid for every place it uses. Please note that I am using Flex 4, and I am ready to use skins, although I do not know if this means that everything that the DataGrid is considering has not yet been fixed.

+3
source share
3 answers

Flex 3 uses textRollOverColorand textSelectedColor, but Flex 4 components no longer support them.

The following example demonstrates all this + adding support for these colors for a spark list:

<s:Application 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";

        global
        {
            textRollOverColor: yellow;
            textSelectedColor: green;
        }

    </fx:Style>

    <fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;

        private function getListDataProvider():ArrayCollection
        {
            return new ArrayCollection([ "Item 1", "Item 2", "Item 3"]);
        }

        private function getGridDataProvider():ArrayCollection
        {
            return new ArrayCollection([ { name: "Item 1" }, { name: "Item 2" }, { name: "Item 3" } ]);
        }

    ]]>
    </fx:Script>

    <s:layout>
        <s:VerticalLayout horizontalAlign="center" verticalAlign="middle"/>
    </s:layout>

    <s:List dataProvider="{getListDataProvider()}"/>

    <s:List dataProvider="{getListDataProvider()}" itemRenderer="ColoredItemRenderer"/>

    <mx:List dataProvider="{getListDataProvider()}"/>

    <mx:DataGrid dataProvider="{getGridDataProvider()}"/>

</s:Application>

ColoredItemRenderer:

<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" 
    autoDrawBackground="true">

    <fx:Script>
    <![CDATA[

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

            var color:uint;
            if (selected)
                color = getStyle("textSelectedColor");
            else if (hovered)
                color = getStyle("textRollOverColor");
            else
                color = getStyle("color");
            sparkLabel.setStyle("color", color);
        }

    ]]>
    </fx:Script>

    <s:Label id="sparkLabel" text="{data}"/>

</s:ItemRenderer>
+3
source

, Flex 4 itemRenders

<s:List itemRenderer="com.renderer.GlossaryRenderer" change="handleGridClick(event)" width="293" height="206" styleName="glossaryList" dataProvider="{_glossary}">
    <s:layout>
        <s:VerticalLayout horizontalAlign="justify" paddingLeft="5" requestedRowCount="9" />
    </s:layout>
</s:List>

<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
            xmlns:s="library://ns.adobe.com/flex/spark" 
            xmlns:mx="library://ns.adobe.com/flex/mx" 
            autoDrawBackground="false">
<s:states>
    <s:State name="normal" />
    <s:State name="hovered" />
    <s:State name="selected" />
</s:states>
<s:Label id="sparkLabel" backgroundColor.selected="#ff0000" color.selected="#FFFFFF" color.hovered="#FFFFFF" text="{data.word}" left="2" right="2" top="4" bottom="4" />
</s:ItemRenderer>
+2

For Spark DataGrid, you need to create a rendering of a new element based on the GridItemRenderer. Then assign this property to the DataGrid itemRenderer.

MyGridItemRender.mxml:

<s:GridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
            xmlns:s="library://ns.adobe.com/flex/spark"  
            width="100%"
            height="100%">

    <fx:Script>
        <![CDATA[

            override public function prepare(hasBeenRecycled:Boolean):void {
                super.prepare(hasBeenRecycled);

                var styleClient:IStyleClient = owner as IStyleClient;
                var color:uint;

                if (selected && styleClient.getStyle("textSelectionColor")!==undefined) {
                    color = styleClient.getStyle("textSelectionColor");
                }
                else if (selected && styleClient.getStyle("textSelectedColor")!==undefined) {
                    color = styleClient.getStyle("textSelectedColor");
                }
                else if (hovered && styleClient.getStyle("textRollOverColor")!==undefined) {
                    color = styleClient.getStyle("textRollOverColor");
                }
                else {
                    color = styleClient.getStyle("color");
                }

                labelDisplay.setStyle("color", color);
            }

        ]]>
    </fx:Script>

    <s:Label id="labelDisplay" 
             paddingLeft="3" paddingRight="3" 
             paddingTop="5" paddingBottom="5"
             verticalCenter="2"
             left="2"/>

</s:GridItemRenderer>

The code:

<s:DataGrid id="dataGrid" itemRenderer="MyGridItemRenderer"/>

You can also use this DataGrid , which is enabled by default.

+1
source

Source: https://habr.com/ru/post/1769668/


All Articles