在项目中使用一个自定义的CheckboxGroup组件继承自Checkbox组件
应用开发的在自定义组件中添加事件,添加属性
package component
{
import flash.events.Event;
import flash.events.MouseEvent;
import mx.controls.CheckBox;
import mx.events.FlexEvent;
import mx.events.ItemClickEvent;
//添加图片图标
[IconFile("GroupCheckBox.png")]
//设置绑定的默认的属性
[DefaultBindingProperty(source="selected", destination="selected")]
//设置绑定的默认的事件
[DefaultTriggerEvent("click")]
//继承几Checkbox类的应用
public class GroupCheckBox extends CheckBox
{
private var _groupName:String;
private var _value:String;
public function GroupCheckBox()
{
//调用父类的方法进行处理化
super();
label = "GroupCheckBox";
//添加事件监听
addEventListener(FlexEvent.ADD, addHandler);
}
private var groupChanged:Boolean = false;
[Inspectable(category="General", defaultValue="checkBoxGroup")]
public function get groupName():String
{
return _groupName;
}
public function set groupName(value:String):void {
if (!value || value == "")
return;
_groupName = value;
groupChanged = true;
invalidateProperties();
invalidateDisplayList();
dispatchEvent(new Event("groupNameChanged"));
}
[Inspectable(category="General", defaultValue="")]
public function get value():String
{
return _value;
}
public function set value(value:String):void {
_value = value;
}
private var _group:CheckBoxGroup;
public function get group():CheckBoxGroup
{
// Debugger asks too soon.
if (!document)
return _group;
if (!_group)
{
if (groupName && groupName != "")
{
try
{
_group = CheckBoxGroup(document[groupName]);
}
catch(err:Error)
{
}
}
}
return _group;
}
public function set group(value:CheckBoxGroup):void
{
_group = value;
}
override protected function commitProperties():void
{
super.commitProperties();
if (groupChanged)
{
addToGroup();
groupChanged = false;
}
}
private function addHandler(event:FlexEvent):void
{
if (!_group && initialized)
addToGroup();
}
private function addToGroup():Object
{
var g:CheckBoxGroup = group; // Trigger getting the group
if (g)
g.addInstance(this);
return g;
}
internal function changeSelected(fireChange:Boolean=true):void {
if (fireChange)
{
dispatchEvent(new Event(Event.CHANGE));
}
}
override protected function clickHandler(event:MouseEvent):void
{
if (!enabled)
return;
if (!_group)
addToGroup();
// Must call super.clickHandler() before setting
//该调用完成selected值设置,并触发change事件
super.clickHandler(event);
if (!_group)
return;
//重新设置组的值
group.itemChange(this);
// Dispatch an itemClick event
var itemClickEvent:ItemClickEvent =
new ItemClickEvent(ItemClickEvent.ITEM_CLICK);
itemClickEvent.label = label;
itemClickEvent.index = group.indexOf(this);
itemClickEvent.relatedObject = this;
itemClickEvent.item = value;
group.dispatchEvent(itemClickEvent);
}
}
}
本类主要的目的讲述关于自定义组件的使用的开发,需要注意的事项的信息。
在一个List