动态添加权限navBarControl1设置 DXperience学习笔记


在进行DXperience进行项目开发的时候 navBarControl1 提供了组分类的功能 相对于微软自带的控件本人认为DXperience有着不可超越的优势,在平常学习的过程中,碰到了一些问题 如何对navBarGroup1进行有选择的显示即对于每个登录的用户进行权限分配,今天把学习的经验写出来,希望能给DXperience爱好的初学者一点启发
  数据库读取到 dataset后进行编写如下代码
 

以下是代码片段:

if (ds.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                         string id = ds.Tables[0].Rows[i]["id"].ToString();  //该用户可管理的id号
                         if (User.Power.IndexOf(id) == -1)
                             continue;
                        string mid = ds.Tables[0].Rows[i]["mid"].ToString(); //隶属的管理类型
                        string name = ds.Tables[0].Rows[i]["PowerName"].ToString(); //管理名称
                        int cls = int.Parse(ds.Tables[0].Rows[i]["type"].ToString());//图片号码 
                        switch (mid)
                        {
                            case "1":
                                AddGroup(navBarGroup1, name, "n" + id, cls);//AddGroup函数用于在navBarGroup1控件中写入新项
                                break;
                            case "2":
                                AddGroup(navBarGroup2, name, "n" + id, cls);
                                break;
                            case "3":
                                AddGroup(navBarGroup3, name, "n" + id, cls);
                                break;
                            case "4":
                                AddGroup(navBarGroup4, name, "n" + id, cls);
                                break;
                            case "5":
                                AddGroup(navBarGroup5, name, "n" + id, cls);
                                break;
                        }


                    }
                }
 

以下是 AddGroup  函数


以下是代码片段:
 ///
        /// 参数传递 添加控件
        ///
        ///
        ///
        ///
        ///
        private void AddGroup(DevExpress.XtraNavBar.NavBarGroup nbg,string name,string id,int cls )
        {
            try 
            {
                DevExpress.XtraNavBar.NavBarItem navBarItem1 = new DevExpress.XtraNavBar.NavBarItem();
                navBarItem1.Caption = name;
                navBarItem1.Name = id;
                navBarItem1.SmallImageIndex = cls;
                navBarItem1.LinkClicked += new DevExpress.XtraNavBar.NavBarLinkEventHandler(navBarItem1_LinkClicked);  //新增项的 点击事件 触发
                nbg.ItemLinks.AddRange(new DevExpress.XtraNavBar.NavBarItemLink[] { new DevExpress.XtraNavBar.NavBarItemLink(navBarItem1) });
            }
            catch (Exception ex)
            {
               MessageBox.Show(ex.Message);
            }
        }


以下是点击事件代码

以下是代码片段:


///
        /// 左侧栏点击事件
        ///
        ///
        ///
        private void navBarItem1_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            try
            {
                string pagename = e.Link.Item.Name; //获取左侧栏的name值
                string caption = e.Link.Caption;    //获取名字
                switch (pagename)
                    {
                        case " ":   
                              //执行代码
                           break;
                     }

           }
            catch (Exception ex)
            {
               MessageBox.Show(ex.Message);
            }
          
        }



Devexpress 之 NavBar 添加/删除

--添加新的ItemLink到 Active Group

int i = 0;

private void button1_Click(object sender, System.EventArgs e) {
    DevExpress.XtraNavBar.NavBarItem item = navBarControl1.Items.Add();
    item.LargeImageIndex = item.SmallImageIndex = imageComboBoxEdit1.SelectedIndex;
    item.Caption = "Item " + (i++).ToString();
    navBarControl1.ActiveGroup.ItemLinks.Add(item);
    ExistSelectedItemLink();
}

--删除当前所选ItemLink

private void button2_Click(object sender, System.EventArgs e) {
    if(navBarControl1.ActiveGroup.SelectedLink != null)
        navBarControl1.ActiveGroup.SelectedLink.Item.Dispose();
    ExistSelectedItemLink();
}

 

--改变样式

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e) {
navBarControl1.View = listBox1.SelectedItem as DevExpress.XtraNavBar.ViewInfo.BaseViewInfoRegistrator;
navBarControl1.ResetStyles();
}


你可能感兴趣的:(动态添加权限navBarControl1设置 DXperience学习笔记)