简体   繁体   English

如何设置Spark DataGrid的标题样式

[英]How to set the header styles of a Spark DataGrid

How do I update the look of the header parts in a Spark DataGrid? 如何更新Spark DataGrid中标题部分的外观? This is a how to community wiki post to set the header background color, header text color, column separator and sort arrow indicator symbol color of a Spark DataGrid. 这是一种如何在社区Wiki帖子中设置Spark DataGrid的标题背景颜色,标题文本颜色,列分隔符和排序箭头指示符符号颜色的方法。

I found a few examples online about how to style the Spark DataGrid header but not in the way I've described. 我在网上找到了一些有关如何设置Spark DataGrid标头样式的示例,但并非以我所描述的方式进行。 So here is how I did it. 这就是我的做法。

MXML : MXML

<s:DataGrid />

CSS : CSS

    s|DataGrid {
        symbolColor:#FF0000; /* defines the sort indicator color */
    }

    s|GridItemRenderer {
        color:#0000FF;       /* defines the header text color */
        chromeColor:#00FF00; /* defines the header background color */
    }

That's it. 而已。 Well, not quite. 好吧,不完全是。 The GridItemRenderer uses gradients. GridItemRenderer使用渐变。 If you do not want those you have to do some extra work in addition to the above. 如果您不想要这些,则除了上述内容之外,还必须做一些额外的工作。

PART II 第二部分
To remove the gradients we have to create two new skin classes. 要删除渐变,我们必须创建两个新的外观类。 A new data grid skin based on spark.skins.spark.DataGridSkin and new data grid header skin based on spark.skins.spark.DefaultGridHeaderRenderer. 基于spark.skins.spark.DataGridSkin的新数据网格外观,以及基于spark.skins.spark.DefaultGridHeaderRenderer的新数据网格头外观。 The first skin extends DataGridSkin. 第一个皮肤扩展了DataGridSkin。 The second skin is a copy of DefaultGridHeaderRenderer. 第二层皮肤是DefaultGridHeaderRenderer的副本。

MXML : MXML

<s:DataGrid skinClass="view.skins.AbstractDataGridSkin"/>

AbstractDataGridSkin : AbstractDataGridSkin

<?xml version="1.0" encoding="utf-8"?>
<spark:DataGridSkin 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx"
    xmlns:fb="http://ns.adobe.com/flashbuilder/2009"
    xmlns:spark="spark.skins.spark.*" 
    xmlns:skins="view.skins.*"
    >

    <fx:Declarations>
        <fx:Component id="headerRenderer">
            <skins:AbstractDataGridHeaderSkin />
        </fx:Component>


    <!--- Change the look of the column separator  -->
    <fx:Component id="columnSeparator">
        <s:Line>
            <s:stroke>
                <s:SolidColorStroke color="0xA6A6A6" weight="1" caps="square"/>
            </s:stroke>
        </s:Line>
    </fx:Component>

    <!--- Defines the look of the header column separator -->
    <fx:Component id="headerColumnSeparator">
        <s:Line>
            <s:stroke>
                <s:SolidColorStroke color="0x00FF00" weight="1" caps="square" />
            </s:stroke>
        </s:Line>
    </fx:Component>
    </fx:Declarations>
</spark:DataGridSkin>

This class points to the new DataGridHeaderSkin. 此类指向新的DataGridHeaderSkin。

AbstractDataGridHeaderSkin : AbstractDataGridHeaderSkin

There is a lot of code in the DefaultGridHeaderRenderer that I don't want to paste into this example so I'm only going to show what's changed. 在DefaultGridHeaderRenderer中有很多代码我不想粘贴到此示例中,因此我仅显示更改内容。

First remove all the Rect layers except "layer 2 fill". 首先删除所有的Rect图层,除了“第2层填充”。 Next, update "layer 2 fill" to a solid color fill as shown below: 接下来,将“第2层填充”更新为纯色填充,如下所示:

<s:GridItemRenderer minWidth="21" minHeight="21"
                xmlns:fx="http://ns.adobe.com/mxml/2009" 
                xmlns:s="library://ns.adobe.com/flex/spark" 
                xmlns:mx="library://ns.adobe.com/flex/mx">

<!-- ...original code here.. -->

<!-- layer 2: fill -->
<!--- @private -->
<!---  Set left and right to -1 to fill out header width 
<s:Rect id="fill" left="0" right="0" top="0" bottom="-1"> -->
<s:Rect id="fill" left="-1" right="-1" top="0" bottom="0">
    <s:fill>
        <s:SolidColor color="0xFFFFFF" color.hovered="0xBBBDBD" color.down="0xAAAAAA" alpha="1" >   
        </s:SolidColor>
    </s:fill>
</s:Rect>

<!-- ...original code here.. -->

</s:GridItemRenderer>

We also have to update the sortIndicatorArrow to remove gradients from there. 我们还必须更新sortIndicatorArrow从那里删除渐变。

<fx:Component id="defaultSortIndicator">
    <s:Path data="M 3.5 7.0 L 0.0 0.0 L 7.0 0.0 L 3.5 7.0" implements="spark.components.gridClasses.IGridVisualElement">
        <fx:Script>
            <![CDATA[
                import spark.components.DataGrid;
                import spark.components.Grid;

                /**
                 *  @private
                 */
                public function prepareGridVisualElement(grid:Grid, rowIndex:int, columnIndex:int):void
                {
                    const dataGrid:DataGrid = grid.dataGrid;
                    if (!dataGrid)
                        return;

                    const color:uint = dataGrid.getStyle("symbolColor");
                    arrowFill.color = color;
                }
            ]]>
        </fx:Script>

        <s:fill>
            <s:SolidColor id="arrowFill" color="0" />
        </s:fill>
    </s:Path>
</fx:Component>

That's it. 而已。

NOTE : If you are using the chrome color it transforms the header fill background. 注意 :如果您使用的是镀铬颜色,它将转换标题填充背景。 You can add the fill to the exclusions (update the updateDisplayList method) or you can set the fill alpha values like this. 您可以将填充添加到排除项中(更新updateDisplayList方法),也可以像这样设置填充alpha值。

<!-- layer 2: fill -->
<!--- @private -->
<!---  Set left and right to -1 to fill out header width 
<s:Rect id="fill" left="0" right="0" top="0" bottom="-1"> -->
<s:Rect id="fill" left="-1" right="-1" top="0" bottom="0">
    <s:fill>
        <s:SolidColor alpha="1" 
                      alpha.hovered=".95"
                      alpha.down="1"/>
    </s:fill>
</s:Rect>

or add this to updateDisplayList and set the colors on the fill (chromeColor would not need to be set then)
var exclusions:Array = [fill, labelDisplay, sortIndicatorInstance];

At some point these common styles will hopefully be available on the Spark DataGrid. 在某些时候,这些常见样式有望在Spark DataGrid上可用。 Spark made it more flexible to define the look and feel of a component which is good but then hardcoded style values into the default skin classes that come with the Flex SDK. Spark使定义组件的外观变得更加灵活,这虽然不错,但随后将样式值硬编码到Flex SDK随附的默认外观类中。 It's now left up to the developer to find and modify these which is not always easy to do. 现在,留给开发人员查找和修改这些内容并不总是那么容易。

To remove the vertical column lines or any other non-required skin part set the property of the part to null in the initialize event like so: 要删除垂直列线或任何其他不需要的外观部件,请在initialize事件中将部件的属性设置为null,如下所示:

AbstractDataGridSkin : AbstractDataGridSkin

<?xml version="1.0" encoding="utf-8"?>
<spark:DataGridSkin 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx"
    xmlns:fb="http://ns.adobe.com/flashbuilder/2009"
    xmlns:spark="spark.skins.spark.*" 
    xmlns:skins="view.skins.*"
    initialize="caretIndicator = null;columnSeparator=null"
    />

To set the height of the header set it on the headerComponent property: 要设置标题的高度,请在headerComponent属性中设置它:

    <fx:Component id="headerRenderer">
        <skins:AbstractDataGridHeaderSkin height="36" />
    </fx:Component>

Sources: http://ramblingdeveloper.com/ramblings/2011/9/25/skinning-a-flex-spark-datagrid-header.html 资料来源: http : //ramblingdeveloper.com/ramblings/2011/9/25/skinning-a-flex-spark-datagrid-header.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM