我尝试做的...
我正在尝试实现更高级的 dropDownList 组件。我添加了一个新属性 selectedValue,它基本上可以取任何值。目前,该组件仅尝试将 selectedValue 与 dataprovider 项的“id”进行匹配。当我调试样本时,它看起来不错,selectedIndex 是根据 selectedValue 设置的。
问题...
启动后 selectedItem 不会显示在 dropDownList 中,仅当我单击下拉按钮时才会出现。表示它已被选中但未在视图中显示。
应用程序启动后...
当我单击自定义组件上的箭头按钮时...
这是代码...
主要的.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Group 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:components="ch.fa.ed.ui.components.*"
width="100%" height="100%" >
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
private var _dpList : ArrayCollection;
public function get dpList() : ArrayCollection {
if (_dpList == null) {
_dpList = new ArrayCollection();
// create items
var person1 : Object = new Object();
person1.id = 10;
person1.name = "Bush";
var person2 : Object = new Object();
person2.id = 12;
person2.name = "Obama";
var person3 : Object = new Object();
person3.id = 30;
person3.name = "Clinton";
_dpList.addItem(person1);
_dpList.addItem(person2);
_dpList.addItem(person3);
}
return _dpList;
}
public function set dpList(dpList : ArrayCollection) : void {
_dpList = dpList;
}
]]>
</fx:Script>
<s:VGroup>
<s:DropDownList id="ddList" dataProvider="{dpList}" labelField="name" selectedIndex="2"/>
<components:EdDropDownList id="ddList2" dataProvider="{dpList}" labelField="name" selectedValue="30"/>
</s:VGroup>
</s:Group>
EdDrowDownList.as
package ch.fa.ed.ui.components {
import mx.collections.IList;
import spark.components.DropDownList;
/**
* @author Michael Wittwer <michael.wittwer@falution.ch>
* @date 20.09.2012
*/
public class EdDropDownList extends DropDownList {
/* ******************************************************************************************************
* fields *
****************************************************************************************************** */
private var _selectedValue : *;
/* ******************************************************************************************************
* member variables *
****************************************************************************************************** */
private var selectedValueChanged : Boolean;
private var dataProviderChanged : Boolean;
public function EdDropDownList() {
super();
}
/*
* overriding the commitProperties method to make sure the selectedValue field gets represented in ui
*/
override protected function commitProperties() : void {
super.commitProperties();
if (selectedValueChanged && dataProviderChanged) {
// find the item mathing selectedValue and set index
if (selectedValue != null && dataProvider != null) {
for (var i : int = 0; i < dataProvider.length; i++) {
var item : * = dataProvider.getItemAt(i);
if (item.id == selectedValue) {
selectedIndex = i;
break;
}
}
}
dataProviderChanged = false;
selectedValueChanged = false;
}
if (selectedValueChanged) {
selectedValueChanged = false;
// find the item mathing selectedValue and set index
if (selectedValue != null && dataProvider != null) {
for (var i : int = 0; i < dataProvider.length; i++) {
var item : * = dataProvider.getItemAt(i);
if (item.id == selectedValue) {
selectedIndex = i;
break;
}
}
}
}
if (dataProviderChanged) {
dataProviderChanged = false;
// find the item mathing selectedValue and set index
if (selectedValue != null && dataProvider != null) {
for (var i : int = 0; i < dataProvider.length; i++) {
var item : * = dataProvider.getItemAt(i);
if (item.id == selectedValue) {
selectedIndex = i;
break;
}
}
}
}
}
/* ******************************************************************************************************
* getter and setter methods *
****************************************************************************************************** */
[Bindable]
public function get selectedValue() : * {
return _selectedValue;
}
public function set selectedValue(selectedValue : *) : void {
_selectedValue = selectedValue;
selectedValueChanged = true;
invalidateProperties();
}
[Bindable]
override public function set dataProvider(value : IList) : void {
super.dataProvider = value;
dataProviderChanged = true;
invalidateProperties();
}
}
任何想法如何解决这一问题?