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()
{
StaticClass.EquipmentTreeViewScroll = scrollOne;
double browseWidth = Application.Current.Host.Content.ActualWidth;//SilverLight控件所在浏览器确定的宽度
double browseHeight = Application.Current.Host.Content.ActualHeight;
double width = browseWidth;
double height = browseHeight;
if (!Application.Current.IsRunningOutOfBrowser)//指示是否从浏览器外状态启动
{
//获取浏览器分辨率信息
ScriptObject screen = (ScriptObject)HtmlPage.Window.GetProperty("screen");
width = (double)screen.GetProperty("width");
height = (double)screen.GetProperty("height");
}
//scrollOne.Height = height - 25 * 3 - 32;
scrollOne.Height = browseHeight - 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)
{
//增加遮罩层
StackPanel stackMark = new StackPanel();
stackMark.Background = new SolidColorBrush(Colors.Black);
stackMark.Opacity = 0.5;
((Grid)StaticClass.UIRoot).Children.Insert(1, stackMark);
((Border)((StackPanel)StaticClass.ProgressBarStatic).Parent).Visibility = Visibility.Visible;
StaticClass.UISB.Begin();
//选项发生变化
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.IsExpanded = true;
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);
}
}
}
}