大家在使用树控件时可能会使用到父节点及节点索引等一些常用属性,在这我们就对树相关的类进行一下扩展.
修改内容仅供学习参考,请大家不要随意对ExtAspNet的源码进行修改,因为咱们要尊重开发者的劳动成果.如果版主觉得可以的话,建议增加一下这些属性方便大家使用,让ExtAspNet越来越好.
ExtAspNet\WebControls\PanelBase.CollapsablePanel.Tree\TreeNode\TreeNode.cs:
(1).增加
/// <summary>
/// 获取当前节点索引值
/// </summary>
public int Index { get; internal set; }
/// <summary>
/// 获取父节点
/// </summary>
public TreeNode ParentNode { get; internal set; }
----------------------------------------------------------------------------------------------------------------------------------------------------
ExtAspNet\WebControls\PanelBase.CollapsablePanel.Tree\Tree.cs:
(1).增加
/// <summary>
/// 获取当前选中的树节点
/// </summary>
public TreeNode SelectedNode
{
get
{
return this.FindNode(this.SelectedNodeID);
}
}
2.ExtAspNet\WebControls\PanelBase.CollapsablePanel.Tree\TreeNode\TreeNodeCollection.cs:
(1).增加定义
TreeNode owner;
(2).修改构造函数为
public TreeNodeCollection(Tree tree, TreeNode owner)
{
this._treeInstance = tree;
this.owner = owner;
}
(3).增加
public new void Add(TreeNode item)
{
item.ParentNode = owner;
item.Index = this.Count;
if(owner!=null)
{
if (owner.Leaf)
{
owner.Leaf = false;
}
}
base.Add(item);
}
public new void Remove(TreeNode item)
{
int index = item.Index;
for (int i = index; i < this.Count; i++)
{
this.Index = this.Index - 1;
}
if (owner != null)
{
if (owner.Nodes.Count==0 && owner.Leaf==false)
{
owner.Leaf = true;
}
}
base.Remove(item);
}
public new void RemoveAt(int index)
{
for (int i = index; i < this.Count; i++)
{
this.Index = this.Index - 1;
}
if (owner != null)
{
owner.Leaf = owner.Nodes.Count == 0 && !owner.Leaf;
}
base.RemoveAt(index);
}
(4).将原有的protected override void InsertItem(int index, TreeNode item)方法修改为
protected override void InsertItem(int index, TreeNode item)
{
if (_treeInstance != null)
{
ResolveTreeNode(item);
}
for (int temp = index; temp < Count; temp++)
{
this[temp].Index = index;
}
item.Index = index;
if (owner != null && owner.Leaf)
{
owner.Leaf = false;
}
base.InsertItem(index, item);
}
|