首页 > 代码库 > Silverlight中后台动态生成树菜单

Silverlight中后台动态生成树菜单

前台

<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  
   x:Class=
"ttt.Leftmenu"
    xmlns=
"http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x=
"http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d=
"http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc=
"http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:controls=
"clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
    mc:Ignorable=
"d"
    d:DesignHeight=
"300" d:DesignWidth="400">
    <Grid x:Name="LayoutRoot" Margin="3">

<!--  树形标签结构定义  -->
        <controls:TreeView x:Name="menu" BorderThickness="0" Background="White" FontSize="12">
        </controls:TreeView>
    </Grid>
</UserControl>

  后台:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ttt{
    public partial class Leftmenu : UserControl
    {
        #region 全局变量

        // 定义按钮点击事件给主页面调用赋值
        public MouseButtonEventHandler OnClick;
        #endregion

        #region 构造方法
        /// <summary>
        /// 构造方法
        /// </summary>
        public Leftmenu()
        {
            InitializeComponent();
            AddTreeNode(0, null);
            this.BindMenuEvent();
        }
        #endregion

        #region  Menu项绑定事件
        /// <summary>
        /// Menu项绑定事件
        /// </summary>
        void BindMenuEvent()
        {
            foreach (TreeViewItem rootItem in this.menu.Items)
            {
                foreach (TreeViewItem item in rootItem.Items)
                {
                    if (item.Tag != null)
                    {
                        item.MouseLeftButtonUp += new MouseButtonEventHandler(item_MouseLeftButtonUp);
                    }
                }
            }
        }
        #endregion

        #region 页面传值
        /// <summary>
        /// 页面传值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void item_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (OnClick != null)
                OnClick(sender, e);
        }
        #endregion

        #region 定义实体类
        /// <summary>
        /// 定义实体类
        /// </summary>
        public class DeciveInfo
        {
            public int DeciveID { getset; }
            public int ParendID { getset; }
            public string DeciveName { getset; }
            public string pageName { getset; }
        }
        #endregion

        #region 获得动态数据的方法
        /// <summary>
        /// 获得动态数据的方法
        /// </summary>
        /// <returns></returns>
        public List<DeciveInfo> GetForumData()
        {
            List<DeciveInfo> deviceInfoList = new List<DeciveInfo>();

            deviceInfoList.Add(new DeciveInfo() { DeciveID = 1, ParendID = 0, DeciveName = "大项一" });
            deviceInfoList.Add(new DeciveInfo() { DeciveID = 2, ParendID = 0, DeciveName = "大项二" });

            deviceInfoList.Add(new DeciveInfo() { pageName = "UserList", DeciveID = 3, ParendID = 1, DeciveName = "大项一中1" });
            deviceInfoList.Add(new DeciveInfo() { pageName = "UserList1", DeciveID = 4, ParendID = 1, DeciveName = "大项一中2" });
            deviceInfoList.Add(new DeciveInfo() { pageName = "UserList", DeciveID = 5, ParendID = 2, DeciveName = "大项二1" });
            deviceInfoList.Add(new DeciveInfo() { pageName = "UserList1", DeciveID = 6, ParendID = 2, DeciveName = "大项二2" });
            return deviceInfoList;
        }
        #endregion

        #region 添加叶子节点信息
        /// <summary>
        ///  添加叶子节点信息
        /// </summary>
        /// <param name="parentID">父亲节点信息</param>
        /// <param name="treeViewItem"></param>
        private void AddTreeNode(int parentID, TreeViewItem treeViewItem)
        {
            // 检索查询的集合
            List<DeciveInfo> result = (from deviceInfo in GetForumData() //forumList
                                       where deviceInfo.ParendID == parentID
                                       select deviceInfo).ToList<DeciveInfo>();

            // 查询结果不为空的情况下进行一下处理
            if (result.Count > 0)
            {
                foreach (DeciveInfo deciveInf in result)
                {
                    TreeViewItem objTreeNode = new TreeViewItem();
                    objTreeNode.Header = deciveInf.DeciveName;
                    // TODO 页面的导向
                    objTreeNode.Tag = deciveInf.pageName;
                    objTreeNode.DataContext = deciveInf;

                    //此样式将会添加的所有叶子结点上
                    objTreeNode.ItemContainerStyle = this.Resources["RedItemStyle"as Style;

                    //添加根节点
                    if (treeViewItem == null)
                    {
                        menu.Items.Add(objTreeNode);
                    }
                    else
                    {
                        treeViewItem.Items.Add(objTreeNode);
                    }
                    //objTreeNode.is
                    AddTreeNode(deciveInf.DeciveID, objTreeNode);
                }
                // 给父亲节点添加属性信息
                //objTreeNode.ItemContainerStyle = this.Resources["RedItemStyle"] as Style; 
            }
        }
        #endregion
    }
}