我想覆盖从祖先组件[flex4]继承的样式的默认值

时间:2012-04-21 16:58:45

标签: actionscript-3 flex styles flex4 custom-component

虽然将[Style ...]元数据标签添加到我的子类确实可以从MXML访问属性 showPromptWhenFocused ,但是initializeStyles()函数不能成功地将默认值更改为true。

我希望用户能够将 showPromptWhenFocused 设置为false,但我希望默认值为true。

package com.santacruzsoftware.crafting.controls
{
import mx.core.FlexGlobals;
import mx.styles.CSSStyleDeclaration;
import mx.styles.StyleManager;

import spark.components.TextInput;

[Style(name="showPromptWhenFocused", inherit="yes", type="Boolean")]

public class WatermarkTextInput extends TextInput
{
    private static function initializeStyles() : void
    {
        var style : CSSStyleDeclaration = FlexGlobals.topLevelApplication.styleManager.getStyleDeclaration("showPromptWhenFocused");
        if (!style)
            style = new CSSStyleDeclaration();

        style.defaultFactory = function() : void
        {
            this.showPromptWhenFocused = true;
        }

        FlexGlobals.topLevelApplication.styleManager.setStyleDeclaration("showPromptWhenFocused", style, false);
    }
    //call the static function immediately after the declaration
    initializeStyles();
}
} 

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

当您拨打getStyleDeclaration()传递您的班级名称时,您可以获得WaterMarkTextInput样式声明:

var style : CSSStyleDeclaration = FlexGlobals.topLevelApplication.styleManager.getStyleDeclaration("WatermarkTextInput");

然后在为您的班级设置样式时执行相同的操作:

FlexGlobals.topLevelApplication.styleManager.setStyleDeclaration("WatermarkTextInput", style, false);

答案 1 :(得分:0)

尝试覆盖notifyStyleChangeInChildren()

package com.santacruzsoftware.crafting.controls {

    import mx.core.FlexGlobals;
    import mx.styles.CSSStyleDeclaration;
    import mx.styles.StyleManager;

    import spark.components.TextInput;

    [Style(name="showPromptWhenFocused", inherit="yes", type="Boolean")]
    public class WatermarkTextInput extends TextInput {

        public var inheritStyles:boolean = true;

        private static function initializeStyles():void {
            var style : CSSStyleDeclaration = FlexGlobals.topLevelApplication.styleManager.getStyleDeclaration("showPromptWhenFocused");
            if (!style)
                style = new CSSStyleDeclaration();

            style.defaultFactory = function():void {
                this.showPromptWhenFocused = true;
            }

            FlexGlobals.topLevelApplication.styleManager.setStyleDeclaration("showPromptWhenFocused", style, false);
        }

        //call the static function immediately after the declaration
        initializeStyles();

        public override function notifyStyleChangeInChildren(styleProp:String, recursive:Boolean):void {
            if (!inheritStyles) {
                switch(styleProp) {
                    case 'showPromptWhenFocused':
                        return;
                }
            }
            super.notifyStyleChangeInChildren(styleProp, recursive);
        }

}