barManager菜单栏的BarButtonItem按钮动态添加下拉菜单

如图效果

barManager菜单栏的BarButtonItem按钮动态添加下拉菜单_第1张图片

一、下拉是一个PopupMenu菜单,所以动态创建一个PopupMenu

//PopupMenu菜单
var popupbbiAll = new PopupMenu(barManager1);

二、创建菜单项按钮参数列表

var listBbiAllParameter = new List
            {
                new BarButtonItemParameter {Caption = "删除当前的" + TotalCount +"条日志", Type = "All", Data = "DeleteCurrent"},
                new BarButtonItemParameter {Caption = "删除超过三月的日志", Type = "All", Data = "DeleteThreeM"},
                new BarButtonItemParameter {Caption = "删除超过一月的日志", Type = "All", Data = "DeleteOneM"},
                new BarButtonItemParameter {Caption = "删除超过半月的日志", Type = "All", Data = "DeleteHalfM"},
                new BarButtonItemParameter {Caption = "删除超过七天的日志", Type = "All", Data = "DeleteSevenD"},
                new BarButtonItemParameter {Caption = "删除超过三天的日志", Type = "All", Data = "DeleteThreeD"},
                new BarButtonItemParameter {Caption = "删除全部日志", Type = "All", Data = "DeleteAll"}
            };


三、遍历列表创建barButtonItem按钮

    1>创建barButtonItem按钮

     2>barButtonItem按钮注册事件

     3>给“删除超过三月的日志”前面添加一条分隔线(这其实是一个分组)

foreach (var barButtonItemParameter in listBbiAllParameter)
            {
                //创建按钮
                var barButtonItem = new BarButtonItem(barManager1, barButtonItemParameter.Caption)
                {
                    Tag = barButtonItemParameter
                };
                //注册事件
                barButtonItem.ItemClick += item_ItemClick;
                //PopupMenu添加按钮与分组
                if (barButtonItemParameter.Data.ToString() == "DeleteThreeM")
                {
                    popupbbiAll.ItemLinks.Add(barButtonItem,true);
                }
                else
                {
                    popupbbiAll.ItemLinks.Add(barButtonItem);
                }                         
            }            

四、barButtonItem又一个属性DropDownControl是一个点击弹出控件,把PopupMenu菜单绑定上去就完成控件制作了。

//bbiDeleteLog为控件名称
bbiDeleteLog.DropDownControl = popupbbiAll;

五、菜单按钮第一项的动态数字是GridView显示数据的条数,可以在必要的地方捕获菜单控件,获取到下拉出的第一项更改Capton属性的值。

var bbiItem = bbiDeleteLog.DropDownControl.Manager.Items;
            foreach(BarItem item in bbiItem)
            {                
                if(item.Name == "")
                {
                    BarButtonItemParameter para = item.Tag as BarButtonItemParameter;
                    switch (para.Data.ToString())
                     {
                         case "DeleteCurrent":
                             //BindingListLogDtos为GridView数据集合
                             para.Caption = "删除当前的" + BindingListLogDtos.Count + "条日志";
                             break;
                        default:
                             break;
                     }
                }
            }

 

 

 

 

 

你可能感兴趣的:(C#)