AS3 禁用 Datagrid 内 textInput 的可编辑/可选择

Posted

技术标签:

【中文标题】AS3 禁用 Datagrid 内 textInput 的可编辑/可选择【英文标题】:AS3 disable editable/selectable for textInput inside of a Datagrid 【发布时间】:2012-03-08 00:49:21 【问题描述】:

我目前正在尝试禁用可选择/可编辑/或将 textInput 更改为动态以获得我想要的结果。

我有一个带有下拉菜单和文本输入区域的自定义数据网格。但是,如果我的 Model # 列中没有数据,我不想在相应的 PurchasePrice 单元格中允许任何条目。

col1 = new DataGridColumn("Model");
    col1.headerText = "Model #";
    c2.consumables_dg.addColumn(col1);
    col1.width = 60;
    col1.editable = false;
    col1.sortable = false;
    col1.cellRenderer = AlternatingRowColors_editNum_PurchasePrice;
col1 = new DataGridColumn("PurchasePrice");
    col1.headerText = "Purchase Price";
    c2.consumables_dg.addColumn(col1);
    col1.width = 60;
    col1.editable = false;
    col1.sortable = false;
    col1.cellRenderer = AlternatingRowColors_editNum_PurchasePrice;

我尝试了 AlternatingRowColors_editNum_PurchasePrice 中的各种项目,但目前似乎没有任何效果。请查看我在 if(__enbaled) 的 else 语句中尝试过的内容

package
    import flash.text.TextFormat;
    import flash.text.TextFormatAlign;
    import fl.controls.TextInput;
    import flash.text.TextFieldType;
    import flash.events.Event;
    import fl.controls.listClasses.ListData;
    import fl.controls.listClasses.ICellRenderer;

public class AlternatingRowColors_editNum_PurchasePrice extends TextInput implements ICellRenderer

    protected var _data:Object;
    protected var _listData:ListData;
    protected var _selected:Boolean;
    private var __enabled:Boolean = false;
    public static var _stage;
    public static var _alignment;
    private var tf:TextFormat;

    public function AlternatingRowColors_editNum_PurchasePrice()
    
        tf = new TextFormat();
        if(__enabled)
                if(_alignment == 2)
                    tf.align = TextFormatAlign.RIGHT;
                else if(_alignment == 1)
                    tf.align = TextFormatAlign.CENTER;
                else
                    tf.align = TextFormatAlign.LEFT;
                
            restrict = "0-9.";
            addEventListener(Event.CHANGE, textChange);

        else
            //this.selectable = false; // did not work
            //textField.selectable = false; // did not work
            //textField.type = dynamic; // did not work
            //textField.type = TextFieldType.DYNAMIC; // did not work
            //this.mouseEnabled = false; // did not work
            //this.tabEnabled = false; // did not work
            //textField.mouseEnabled = false; // did not work
            //textField.tabEnabled = false; // did not work
            //selectable = false; // did not work
            //this.selectable = false; // did not work
            //_enabled = false; // did not work
            //-----------------------------------------------------------
            //   *** Corresponding Entry to enable was placed above ***
            //-----------------------------------------------------------
        
        super();
    

    public function textChange(Event):void
        //trace(_data.Discount);
        _data.PurchasePrice = text;
    

    public function get data():Object
    
        return _data;
    

    public function set data(value:Object):void
    
        _data = value;
        text = value.PurchasePrice;

        if(value.Model != "") __enabled = true;

        if (value.id % 2 == 0) 
            setStyle("upSkin", AlternateColor1 );
         else 
            setStyle("upSkin", AlternateColor2 );
        
    

    public function get listData():ListData
    
        return _listData;
    

    public function set listData(value:ListData):void
    
        _listData = value;
    

    public function get selected():Boolean
    
        return _selected;
    

    public function set selected(value:Boolean):void
    
        _selected = value;
    

    public function setMouseState(state:String):void
    
    
    override protected function drawLayout():void
    
        textField.setTextFormat(tf);
        super.drawLayout();
    



我只是以错误的方式接近这个吗?与其尝试停止鼠标、制表符或将文本字段转换为动态,我应该尝试其他方法以获得所需的结果吗?

谢谢, jc

【问题讨论】:

【参考方案1】:

最简单的方法是更改​​TextField 类型本身,然后更改它的selectable 属性:

//disable input
tf.selectable = false;
tf.type = TextFieldType.DYNAMIC;

//enable input
tf.selectable = true;
tf.type = TextFieldType.INPUT;

也不要忘记根据您的字段的需要/需要设置mouseEnabledtabEnabled 属性。

【讨论】:

如果我像你一样使用TextFieldType,它会给我一个错误。我用这段代码而不是你的代码更正了它:tf.type = "dynamic";txtAnswer.type = "input"; 可能对其他人有用;)【参考方案2】:

确保将类/页面/框架顶部的 TextFieldType 导入为:

import flash.text.TextFieldType;

【讨论】:

以上是关于AS3 禁用 Datagrid 内 textInput 的可编辑/可选择的主要内容,如果未能解决你的问题,请参考以下文章

as3 重用具有不同变量的 datagrid cellrenderer

如何禁用 DataGrid 的“全选”按钮

WPF Datagrid行上下文菜单-禁用菜单项[关闭]

wpf:DataGrid 禁用选定的行样式 - 或行选择

WPF datagrid:禁用某些行中的编辑

禁用 WPF MVVM 中 DataGrid 中自动生成的列的排序