EquipmentTreeView.xaml_1118

前台

<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="ComputerDropDragControl.EquipmentTreeView"
    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"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <ScrollViewer x:Name="scrollOne"  HorizontalScrollBarVisibility="Auto" Width="196"
                                      VerticalScrollBarVisibility="Auto"
                  VerticalAlignment="Top" HorizontalAlignment="Left" Background="#333333">
        <Grid x:Name="LayoutRoot" Width="auto" Height="auto" HorizontalAlignment="Left" VerticalAlignment="Top">
            <sdk:TreeView x:Name="equipmentTree">
            </sdk:TreeView>
        </Grid>
    </ScrollViewer>
</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;
using System.Collections.ObjectModel;
using System.Windows.Browser;

namespace ComputerDropDragControl
{
    /// <summary>
    /// 设备树
    /// 创建人:吴兆娟
    /// 创建时间:2011-10-24
    /// </summary>
    public partial class EquipmentTreeView : UserControl
    {
        ObservableCollection<Equipment> eList;

        #region <<加载事件>>

        /// <summary>
        /// 初始化“设备树”
        /// </summary>
        public EquipmentTreeView(UIElement uc)
        {
            InitializeComponent();

            BackElement = uc;

            LoadData();

            //绑定树
            BindTree("0", null);

            //初始化界面
            InitForm();

            //订阅“选项发生变化”事件
            equipmentTree.SelectedItemChanged += new RoutedPropertyChangedEventHandler<object>(equipmentTree_SelectedItemChanged);
        }


        /// <summary>
        /// 初始化界面
        /// </summary>
        private void InitForm()
        {
            //获取浏览器分辨率信息
            ScriptObject screen = (ScriptObject)HtmlPage.Window.GetProperty("screen");
            double width = (double)screen.GetProperty("width");
            double height = (double)screen.GetProperty("height");
            scrollOne.Height = height - 25 * 3 - 32;

            TreeViewItem temp = (TreeViewItem)equipmentTree.Items[0];
            temp.IsSelected = true;
            Equipment entity = (Equipment)temp.DataContext;
            returnID = entity.ID;
            BindControl(entity.IsRoom, entity.ID);
        }

        #endregion

        #region <<控件事件>>

        /// <summary>
        /// “选项”发生变化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void equipmentTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeView tree = sender as TreeView;
            TreeViewItem treeViewItem = tree.SelectedItem as TreeViewItem;

            returnID = ((Equipment)treeViewItem.DataContext).ID;
            BindControl(((Equipment)treeViewItem.DataContext).IsRoom, ((Equipment)treeViewItem.DataContext).ID);

        }

        #endregion

        #region <<辅助方法>>

        /// <summary>
        /// 递归绑定树
        /// </summary>
        /// <param name="root"></param>
        /// <param name="xmlDoc"></param>
        private void BindTree(string parentID, TreeViewItem treeViewItem)
        {
            List<Equipment> result = (from equipmentEntity in eList
                                      where equipmentEntity.ParentID == parentID
                                      select equipmentEntity).ToList<Equipment>();
            if (result.Count > 0)
            {
                foreach (Equipment equipment in result)
                {
                    TreeViewItem temp = new TreeViewItem();
                    temp.Header = equipment.Name;
                    temp.DataContext = equipment;

                    if (treeViewItem == null)
                    {
                        equipmentTree.Items.Add(temp);
                    }
                    else
                    {
                        treeViewItem.Items.Add(temp);
                    }
                    BindTree(equipment.ID, temp);
                }
            }
        }

        /// <summary>
        /// “设备类”
        /// </summary>
        private class Equipment
        {
            public Equipment() { }
            public Equipment(string id, string parentID, string name, string description, bool isRoom)
            {
                ID = id;
                ParentID = parentID;
                Name = name;
                Description = description;
                IsRoom = isRoom;
            }

            public string ID { get; set; }
            public string ParentID { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
            public bool IsRoom { get; set; }
        }

        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        private ObservableCollection<Equipment> LoadData()
        {
            eList = new ObservableCollection<Equipment>{
                new Equipment("1","0","园区机房","园区机房",true),
                new Equipment("2","1","园区机房排一","园区机房排一",false),
                new Equipment("3","1","园区机房排二","园区机房排二",false),
                new Equipment("4","1","园区机房排三","园区机房排三",false),
                new Equipment("5","0","市区机房","市区机房",true),
                new Equipment("6","5","市区机房排一","市区机房排一",false),
                new Equipment("7","5","市区机房排二","市区机房排二",false),
                new Equipment("8","0","新区机房","新区机房",true),
                new Equipment("9","8","新区机房排一","新区机房排一",false),
                new Equipment("10","8","新区机房排二","新区机房排二",false),
                new Equipment("11","8","新区机房排三","新区机房排三",false),
                new Equipment("12","8","新区机房排四","新区机房排四",false)
            };

            return eList;
        }


        //备注
        //http://www.cnblogs.com/daizhj/archive/2009/02/02/1372088.html

        #endregion

        private string returnID;

        /// <summary>
        /// 返回“选中项”的ID
        /// </summary>
        public string ReturnID
        {
            get
            {
                return returnID;
            }
        }

        /// <summary>
        /// 客户端与控件交互的控件
        /// </summary>
        public UIElement BackElement { get; set; }

        public void BindControl(bool isRoom, string ID)
        {
            ((StackPanel)BackElement).Children.Clear();
            if (isRoom)
            {
                ComputerRoom a = new ComputerRoom(ID, BackElement);
                ((StackPanel)BackElement).Children.Add(a);
            }
            else
            {
                ComputerRack a = new ComputerRack(ID);
                ((StackPanel)BackElement).Children.Add(a);
            }

        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值