菜单中的分割线: 通过将 NativeMenuItem 构造函数中的 isSeparator 属性设置为 true 创建一条分隔线。
_menu = new NativeMenu(); _menuAddTopOU = new NativeMenuItem(RM.getString(BUNDLE_OUSETUP, "ous.menu.addTopOU")); _menuAdd = new NativeMenuItem(); _menuEdit = new NativeMenuItem(); _menuRemove = new NativeMenuItem(); _separator = new NativeMenuItem(null, true); //分割线 _menu.addItem(_menuAddTopOU); _menu.addItem(_separator); _menu.addItem(_menuAdd); _menu.addItem(_menuEdit); _menu.addItem(_menuRemove); _menu.addEventListener(ContextMenuEvent.MENU_SELECT, onMenuSelected); _menu.addEventListener(Event.SELECT, onMenuItemSelected); _treeOUs.contextMenu = _menu;
很多时候为了获得对组件的灵活控制, 不得不放弃MXML, 直接使用ActionScript.
//新建一个DataGrid _dataGrid = new DataGrid(); //增加右键 var menu:NativeMenu = new NativeMenu(); var menuItemDelete:NativeMenuItem = new NativeMenuItem("Delete"); //监听事件,在右键选定Delete时运行该函数 menuItemDelete.addEventListener(Event.SELECT, onMenuDeleteClicked); //将该menuItem加入到Menu中 menu.addItem(menuItemDelete); _dataGrid.contextMenu = menu; //将菜单加入到DataGrid中 columnLocale = new DataGridColumn(); //新建一个列 columnLocale.dataField ="locale";//设定DataGrid中locale列的EditItem; comboBoxLocaleEditor = new ClassFactory(ComboBox); // D comboBoxLocaleEditor.properties = {dataProvider : LocalizationItem.localeArray} //设定该EditorItem的属性 columnLocale.itemEditor = comboBoxLocaleEditor; var columnGender:DataGridColumn = new DataGridColumn(); columnGender.headerText = "Gender""; columnGender.dataField = "Gender_"; //使用ItemRender columnGender.itemRenderer = new ClassFactory(Gender_Label); _dataGrid.showHeaders = false; //隐藏DataGrid的Header _dataGrid.columns = [columnLocale, columnGender]; //这一个数组,存放DataGrid的Column _dataGrid.dataProvider = value; //设定dataProvider
具体在实际中的使用, 无异于MXML.
问题: 在很多情况下, 我们在DataGrid, List, Tree等容器中使用右键, 进行如修改, 删除 某行的操作. 问题是如果该容器初始状态下直接进行右键点击时, 并不能选定任何数据.
解决: 通过监听右键菜单SELECT事件, 获取到当前右键所击位置的Index, 并将之赋值给DataGrid或其他容器的SelectIndex, 完成点击操作
详细实现: 以DataGrid为例.
第一步, 首先给DataGrid添加菜单
var dgMenu:NativeMenu = new NativeMenu(); var del:NativeMenuItem = new NativeMenuItem("Test"); del.addEventListener(Event.SELECT, onRightClickDel); dgMenu.addItem(del); dgMenu.addEventListener(ContextMenuEvent.MENU_SELECT, onRightClicked); _dataGridStudent.contextMenu = dgMenu;
第二步, 响应函数:
/**响应右键点击 */ private function onRightClicked(e:ContextMenuEvent):void { var rightClickItemRender:IListItemRenderer; if(e.mouseTarget is IListItemRenderer) { rightClickItemRender = IListItemRenderer(e.mouseTarget); }else if(e.mouseTarget.parent is IListItemRenderer) { rightClickItemRender = IListItemRenderer(e.mouseTarget.parent); } if(rightClickItemRender != null) { var rightClickIndex:int = _dataGridStudent.itemRendererToIndex(rightClickItemRender); if(_dataGridStudent.selectedIndex != rightClickIndex) { _dataGridStudent.selectedIndex = rightClickIndex; onSlectedChange(); } } trace("通过右键单击获得选定的行: " + _dataGridStudent.selectedIndex); }
Delete响应函数:
/** 当右键菜单的Delete点击后响应*/ private function onRightClickDel(e:Event):void { //在这里放逻辑.... }
效果:
控制台Trace信息:
通过右键单击获得选定的行: 1
参考资料: Adobe CookBook Select List item with mouse right-click
// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.