前台:
<StackPanel x:Name="Nav" Width="245" DockPanel.Dock="Top" Margin="0,5,0,0"> </StackPanel>
后台:构建树形菜单
public void BindBorder()
{
#region 数据集
//获取缓存中所有模块
List<Schema.FrameAppModuleInfo> SystemModules = GlobalRepertories.SystemModules;
//获取所有非空节点
var ls2 = from itemGroup in SystemModules
where itemGroup.ParentGuid != ""
group itemGroup by itemGroup.ParentGuid
into g
select new { ParentGuid = g.Key };
var FatherList = ls2.ToList();
//获取为空节点(空节点为根目录)
var resultList = from item in SystemModules
where item.ParentGuid.ToLower() == ""
select item;
var escortList = resultList.ToList();
#endregion
#region 加载根目录
//创建根目录
foreach (Schema.FrameAppModuleInfo ThisModuleInfo in escortList)
{
//创建可展开模块
System.Windows.Controls.Expander MyExpander = new Expander();
//将唯一标示赋值到Tag
MyExpander.Tag = ThisModuleInfo.RowGuid.ToString();
//动态加载点击事件
MyExpander.Expanded += new RoutedEventHandler(Expander_Expanded);
MyExpander.Uid = ThisModuleInfo.WindowsName.ToString();
//构建标题层
System.Windows.Controls.HeaderedContentControl MyHeadered = new HeaderedContentControl();
//构建标题层容器
System.Windows.Controls.StackPanel MyStackPanel = new StackPanel();
//构建图片容器
System.Windows.Controls.TextBlock MyTextBlock = new TextBlock();
//构建文字容器
System.Windows.Controls.TextBlock MyTextBlock2 = new TextBlock();
//加载标题层容器样式
Style StackPanelStyle = (Style)this.FindResource("horStackpanel");
//加载图片容器样式
Style TextBlockStyle = (Style)this.FindResource("NavIcon");
//获取图标字体赋值到图片容器
MyTextBlock.Text = System.Web.HttpUtility.HtmlDecode(Convert.ToString(ThisModuleInfo.ModuleFont));
//加载样式
MyTextBlock.Style = TextBlockStyle;
//标题文字赋值到文字容器
MyTextBlock2.Text = ThisModuleInfo.ModuleName;
//标题层容器加载样式
MyStackPanel.Style = StackPanelStyle;
//标题层容器添加控件
MyStackPanel.Children.Add(MyTextBlock);
//标题层容器添加控件
MyStackPanel.Children.Add(MyTextBlock2);
//头部容器填充标题层容器
MyHeadered.Content = MyStackPanel;
MyExpander.Header = MyHeadered;
Nav.Children.Add(MyExpander);
}
#endregion
#region 加载子目录
//获取已经添加的所有根节点容器
for (int i = 0; i < Nav.Children.Count; i++)
{
//查找控件类型为System.Windows.Controls.Expander
if (Nav.Children[i].GetType().ToString() == "System.Windows.Controls.Expander")
{
//实例化控件
System.Windows.Controls.Expander MyExpander = Nav.Children[i] as System.Windows.Controls.Expander;
//判断Tag是否有数据
if (!string.IsNullOrEmpty(Convert.ToString(MyExpander.Tag)))
{
//创建子菜单容器
System.Windows.Controls.StackPanel MyStackPanel = new StackPanel();
//加载子菜单容器样式
Style StackPanelStyle = (Style)this.FindResource("expanderChildBg");
MyStackPanel.Style = StackPanelStyle;
//便利所有为子节点的集合
for (int m = 0; m < FatherList.Count; m++)
{
//当前父节点的TAG是否等于节点唯一标示
if (FatherList[m].ParentGuid.ToUpper() == Convert.ToString(MyExpander.Tag).ToUpper())
{
//便利所有菜单集合
foreach (Schema.FrameAppModuleInfo ThisModuleInfo in SystemModules)
{
if (FatherList[m].ParentGuid.ToUpper() == ThisModuleInfo.ParentGuid.ToUpper())
{
//子节点按钮容器样式
Style StackPanelButtonStyle = (Style)this.FindResource("horStackpanel");
//子节点按钮样式
Style ButtonStyle = (Style)this.FindResource("sideBtn");
//子节点图标字体样式
Style TextBlockStyle = (Style)this.FindResource("childNavIcon");
//子节点按钮容器
System.Windows.Controls.StackPanel MyStackPanelButton = new StackPanel();
//子节点按钮
System.Windows.Controls.Button MyButton = new Button();
//子节点图标容器
System.Windows.Controls.TextBlock MyTextBlock = new TextBlock();
//子节点文字标题容器
System.Windows.Controls.TextBlock MyTextBlock2 = new TextBlock();
//动态加载按钮事件
MyButton.Click += new RoutedEventHandler(Button_Click);
//动态加载按钮样式
MyButton.Style = ButtonStyle;
//动态加载窗体链接地址
MyButton.Tag = ThisModuleInfo.WindowsName;
MyButton.Uid = ThisModuleInfo.RowGuid;
//动态加载图标字体容器样式
MyTextBlock.Style = TextBlockStyle;
//动态加载图标字体
MyTextBlock.Text = System.Web.HttpUtility.HtmlDecode(Convert.ToString(ThisModuleInfo.ModuleFont));
//动态加载按钮样式
MyStackPanelButton.Style = StackPanelButtonStyle;
//动态加载菜单文字标题
MyTextBlock2.Text = ThisModuleInfo.ModuleName;
//填充按钮容器
MyStackPanelButton.Children.Add(MyTextBlock);
//填充按钮容器
MyStackPanelButton.Children.Add(MyTextBlock2);
//填充按钮
MyButton.Content = MyStackPanelButton;
//填充子菜单容器
MyStackPanel.Children.Add(MyButton);
}
}
//填充父菜单
MyExpander.Content = MyStackPanel;
}
}
}
}
}
#endregion
}