新建一个项目,名称为TreeViewMemo;在Form1窗体上添加控件DockManager和treelist,作为导航菜单,并可以实现左侧停靠及关闭导航菜单,关闭后用主菜单按钮打开;imageCollection为导航节点添加节点图标,xtraTabbedMdiManager作为显示打开窗体的容器;最终结果如下图:
添加引用,此处用的Oracle数据库;
using Oracle.ManagedDataAccess.Client;
using System.Reflection;
写一个方法用于调用查询系统导航菜单的结构
private string connecting = "数据库连接字符串";
private DataTable GetTable(string sql) //获取查询的返回结果集
{
DataTable dt = new DataTable();
OracleConnection con = new OracleConnection(connecting);
con.Open();
OracleDataAdapter oda = new OracleDataAdapter(sql,con);
oda.Fill(dt);
con.Close();
return dt;
}
利用Form1的Load事件为为TreeList添加数据源,并设置Treelist的属性值,代码如下:
private void Form1_Load(object sender, EventArgs e)
{
/在每个打开页面上都多一个关闭的小叉子x
this.xtraTabbedMdiManager1.ClosePageButtonShowMode = DevExpress.XtraTab.ClosePageButtonShowMode.InActiveTabPageHeader;
//当打开的窗体大于1个的时候,可以拖拽一个处于悬浮状态,也可以拖拽回到MDI父容器中
this.xtraTabbedMdiManager1.FloatOnDoubleClick = DevExpress.Utils.DefaultBoolean.True;
this.xtraTabbedMdiManager1.FloatOnDrag = DevExpress.Utils.DefaultBoolean.True;
this.xtraTabbedMdiManager1.FloatPageDragMode = DevExpress.XtraTabbedMdi.FloatPageDragMode.FullWindow;
//获取数据库菜单
string sql = " SELECT * FROM DP_SYSLIST order by 1";
DataTable dt = new DataTable();
dt = GetTable(sql);
treeList1.DataSource = dt;
treeList1.KeyFieldName = "SYS_ID";//父级字段名称
treeList1.ParentFieldName = "SYS_LEVEL";//展开所有节点
//treeList1.ExpandAll(); //展开子节点
//treeList1.Appearance.FocusedCell.BackColor = Color.LightSteelBlue; //焦点行颜色渐变
// treeList1.Appearance.FocusedCell.BackColor2 = Color.SteelBlue;
//treeList1.OptionsView.ShowHorzLines = false; //隐藏行列边框
//treeList1.OptionsView.ShowVertLines = false; //隐藏垂直表格线
treeList1.OptionsView.FocusRectStyle = DevExpress.XtraTreeList.DrawFocusRectStyle.None; //隐藏焦点行边框
treeList1.LookAndFeel.UseWindowsXPTheme = true; //使用XP主题
treeList1.LookAndFeel.UseDefaultLookAndFeel = true; //禁用默认外观与感觉
treeList1.OptionsView.ShowIndicator = false; //隐藏节点指示器面板 显示序号的地方
treeList1.OptionsBehavior.Editable = false; //禁止编辑
//treeList1.TreeLineStyle = DevExpress.XtraTreeList.LineStyle.Percent50; //设置树线的样式
//treeList1.OptionsView.ShowColumns = false; //隐藏列名
treeList1.OptionsMenu.EnableColumnMenu = false; //右键点击字段值不显示菜单
treeList1.OptionsCustomization.AllowSort = false; //点击字段值排序
treeList1.OptionsCustomization.AllowFilter = false; //字段值右上角的沙漏,过滤
treeList1.Appearance.FocusedCell.BackColor = System.Drawing.Color.LightSteelBlue; //设置选中行的背景色
// treeList1.Appearance.FocusedCell.Options.UseBackColor = true;
treeList1.OptionsCustomization.AllowColumnMoving = false; //禁止拖拽表头 字段值
//treeList1.OptionsView.ShowColumns = false; //隐藏列标头
treeList1.OptionsView.ShowIndentAsRowStyle = true;
}
导航菜单的表结构
SYS_MODEL为窗体名称,SYS_PATH为名称空间名称,也就是项目名称;
添加一个控件dockManager,做为导航菜单,可以实现做隐藏,设置dockmanager的属性
this.dockManager1.AutoHiddenPanelShowMode = DevExpress.XtraBars.Docking.AutoHiddenPanelShowMode.MouseHover;
this.dockManager1.DockingOptions.AllowRestoreToAutoHideContainer = true;
this.dockManager1.DockingOptions.HideImmediatelyOnAutoHide = true;
点击Add Panel at Left ,添加一个dockPanel1,设置dockpanel1的属性;
this.dockPanel1.Dock = DevExpress.XtraBars.Docking.DockingStyle.Left;
this.dockPanel1.Text = "导航菜单";
将Treelist拖到dockPanel1中,设置treelist的属性
this.treeList1.Dock = System.Windows.Forms.DockStyle.Fill;
this.treeList1.OptionsView.ShowColumns = false; //隐藏标题列头字段值
在TreeList的属性中添加一列值,作为显示导航列
并为TreeList属性SelectImageList选择刚刚添加的控件imageCollection1;
设置imageCollection1的属性,添加图标,点击Images,弹出的界面中添加图标
设置TreeLsit的GetSelectImage事件,代码如下:
private void treeList1_GetSelectImage(object sender, DevExpress.XtraTreeList.GetSelectImageEventArgs e)
{
if (e.Node.Expanded == false) //如果不是展开状态
{
e.NodeImageIndex = 3; //显示索引为3的图标
}
else //否则为展开状态
{
e.NodeImageIndex = 4; //显示索引为4的图标
}
if (e.Node.FirstNode == null) //如果为最后一个节点,没有子节点
{
e.NodeImageIndex = 5; //显示为索引5的的图标
}
}
最后做TreeList的DoubleClick事件,实现双击节点打开窗体,并在父容器中显示,代码如下:
private void treeList1_DoubleClick(object sender, EventArgs e)
{
TreeListNode clickedNode = this.treeList1.FocusedNode;
if (clickedNode.FirstNode == null)
{
try
{
string strFunNo = clickedNode.GetValue("SYS_MODEL").ToString();
//string path = Assembly.GetExecutingAssembly().GetName().Name; //获取命名空间名称
string path= clickedNode.GetValue("SYS_PATH").ToString();
string name = path + "." + strFunNo;
//Assembly assembly = Assembly.GetExecutingAssembly();
// 实例化窗体,利用反射打开窗体
// Form form = assembly.CreateInstance(name) as Form;
Form form = Assembly.Load(path).CreateInstance(name) as Form;
// form.Show();
//判断窗体是否打开,打开则跳转,未打开则打开
bool flag = false;
foreach (Form form2 in base.MdiChildren)
{
bool flag2 = form.Name == form2.Name;
if (flag2)
{
form2.Activate();
flag = true;
form.Dispose();
break;
}
}
bool flag3 = !flag;
if (flag3)
{
form.MdiParent = this;
form.FormBorderStyle = FormBorderStyle.None;
form.Dock = DockStyle.Fill;
form.Show();
}
}
catch
{
MessageBox.Show("当前菜单没有启用");
}
}
}
//重新加载菜单的代码
private void ResToolStripMenuItem_Click(object sender, EventArgs e)
{
treeList1.CollapseAll();//折叠全部节点
//重新加载的菜单
string sql = " SELECT * FROM DP_SYSLIST order by 1";
DataTable dt = new DataTable();
dt = GetTable(sql);
treeList1.DataSource = dt;
treeList1.KeyFieldName = "SYS_ID";//父级字段名称
treeList1.ParentFieldName = "SYS_LEVEL";//展开所有节点
//关闭全部窗体,除去Form1,Form1为主窗体
for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
{
if (Application.OpenForms[i].Name != "Form1")
{
Application.OpenForms[i].Close();
}
}
}
private void ShowMenuToolStripMenuItem_Click(object sender, EventArgs e)
{ //显示菜单的代码
this.dockPanel1.Show();
this.dockPanel1.Dock = DevExpress.XtraBars.Docking.DockingStyle.Left;
}