简体   繁体   English

Flex扩展ComboBox

[英]Flex extending ComboBox

I created a class CustomCombo.as that extends ComboBox . 我创建了一个扩展ComboBox的类CustomCombo.as What is happening is that the CustomCombo combobox is showing as being editable. 发生的事情是CustomCombo组合框显示为可编辑。 I do not want this and I cant find the properties to set the editable to false . 我不想要这个,我无法找到将editable设置为false的属性。

I also tried setting the combobox's textInput.editable control to false , but to no avail. 我也尝试将组合框的textInput.editable控件设置为false ,但无济于事。

Any help would be greatly appreciated. 任何帮助将不胜感激。

CustomCombo.as

package custom {

    import spark.components.ComboBox;

    public class CustomCombo extends ComboBox {

        public function CustomCombo() {
            super();
//          this.editable = false; //<-- THIS DOESNT WORK   ***Access of possibly undefined property editable through a reference with static type custom:CustomCombo
//          this.textInput.editable = false; //<-- THIS DOESNT WORK   ***Cannot access a property or method of a null object reference
        }
    }
}

After rummaging through the Flex 4 API I found that they suggest to use the DropDownList control. 通过Flex 4 API翻找后,我发现他们建议使用DropDownList控件。 From what I can see is that they removed the editable property from the ComboBox control in Flex 4, but I may be wrong. 从我可以看到他们从Flex 4中的ComboBox控件中删除了editable属性,但我可能错了。

I implemented DropDownList and that solved my problem. 我实现了DropDownList ,这解决了我的问题。

I see that you're using spark and not mx. 我看到你使用的是火花而不是mx。 The editable property I referred to is applicable only to mx based list. 我引用的editable属性仅适用于基于mx的列表。 In spark, ComboBox extends DropDownListBase and is editable by default. 在spark中, ComboBox扩展了DropDownListBase ,默认情况下是可编辑的。

The ComboBox control is a child class of the DropDownListBase control. ComboBox控件是DropDownListBase控件的子类。 Like the DropDownListBase control, when the user selects an item from the drop-down list in the ComboBox control, the data item appears in the prompt area of the control. DropDownListBase控件一样,当用户从ComboBox控件的下拉列表中选择项时,数据项将显示在控件的提示区域中。

One difference between the controls is that the prompt area of the ComboBox control is implemented by using the TextInput control, instead of the Label control for the DropDownList control. 控件之间的一个区别是ComboBox控件的提示区域是使用TextInput控件实现的,而不是DropDownList控件的Label控件。 Therefore, a user can edit the prompt area of the control to enter a value that is not one of the predefined options. 因此,用户可以编辑控件的提示区域以输入不是预定义选项之一的值。

For example, the DropDownList control only lets the user select from a list of predefined items in the control. 例如, DropDownList控件仅允许用户从控件中的预定义项列表中进行选择。 The ComboBox control lets the user either select a predefined item, or enter a new item into the prompt area. ComboBox控件允许用户选择预定义项目,或在提示区域中输入新项目。 Your application can recognize that a new item has been entered and, optionally, add it to the list of items in the control. 您的应用程序可以识别已输入新项目,并可选择将其添加到控件中的项目列表中。

The ComboBox control also searches the item list as the user enters characters into the prompt area. 当用户在提示区域中输入字符时, ComboBox控件还会搜索项目列表。 As the user enters characters, the drop-down area of the control opens. 当用户输入字符时,控件的下拉区域将打开。 It then and scrolls to and highlights the closest match in the item list. 然后滚动并突出显示项目列表中最接近的匹配项。

So ideally, you should be using DropDownList in this case. 理想情况下,在这种情况下你应该使用DropDownList

You're getting null error when trying to access textInput from the constructor because it hasn't been created yet. 尝试从构造函数访问textInput时,您会收到null错误,因为它尚未创建。 In mx based controls (Flex-3), you can access it from the creationComplete handler; 在基于mx的控件(Flex-3)中,您可以从creationComplete处理程序访问它; I'm not quite sure how to do it for spark based controls. 我不太清楚如何做基于火花的控制。

Update: I think I've figured out how to access skin parts in spark (though you might wanna use the DropDownBox instead). 更新:我想我已经想出如何访问spark中的皮肤部分(尽管你可能想要使用DropDownBox )。 You have to override the partAdded method. 您必须覆盖partAdded方法。

override protected function partAdded(partName:String, instance:Object):void
{
    super.partAdded(partName, instance);
    if (instance == textInput)
    {
        textInput.editable = false;
    }
}

There's one catch though: it may not work in this case. 虽然有一个问题:在这种情况下它可能不起作用。 The source code of ComboBox.as says that ComboBox.as源代码说明了这一点

the API ignores the visual editable and selectable properties API忽略可视的editableselectable属性

So DropDownList it is! 所以它是DropDownList


Initial answer, posted for mx ComboBox . 初步回答,发布于mx ComboBox

This shouldn't happen as the default value of the editable property is false . 这不应该发生,因为editable属性的默认值为false

Try explicitly setting the value to false from the constructor. 尝试从构造函数显式将值设置为false

public function CustomCombo() {
  super();
  this.editable = false;
}

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

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