0

虽然将 [Style ...] 元数据标记添加到我的子类确实使属性showPromptWhenFocused可以从 MXML 访问,但 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();
}
} 

有任何想法吗?

4

2 回答 2

0

当您getStyleDeclaration()在类名中调用 pass 时,您可以获得 WaterMarkTextInput 样式声明:

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

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

FlexGlobals.topLevelApplication.styleManager.setStyleDeclaration("WatermarkTextInput", style, false);
于 2012-04-21T18:05:21.183 回答
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);
        }

}
于 2016-04-07T22:17:01.617 回答