ObservableCollection集合 提供基本的数据更改通知,即通知数据更改到UI界面。具体参见:点击打开链接。 比起常规的集合对象List,DataTable,Dictionary等,优势相当明显,就是更新集合数据之后不需要重新绑定到控件显示(针对WPF项目并使用Binding模式)。本事例演示了如果使用它来进行treeview节点的新增,上移,下移以及从集合中移除功能。
新增功能:必须选择节点后执行新增,可以无限增加子节点。
上移功能:只能在同一个父节点下上移。
下移功能:只能在同一个父节点下下移。
移除功能:必须选择节点后执行删除。
1:构建数据类
数据类的构建取决于TreeView的数据结构,即存在“父子”关系。那么该类中就需要包含该节点信息,该节点父节点信息,该节点的同级上节点,同级下节点。
基础数据类 /// <summary>
/// 基础数据类
/// </summary>
public class BaseEntity
{
private Guid _ID = Guid.Empty;
public Guid ID
{
get { return _ID; }
set { _ID = value; }
}
public BaseEntity()
{
this._ID = Guid.NewGuid();
}
public BaseEntity(Guid guid)
{
this._ID = guid;
}
#region 实现INotifyPropertyChanged 接口
public event PropertyChangedEventHandler PropertyChanged;
public void OnChangedProperty(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
基础树类
/// <summary>
/// 基础数据类
/// </summary>
public class BaseEntity
{
private Guid _ID = Guid.Empty;
public Guid ID
{
get { return _ID; }
set { _ID = value; }
}
public BaseEntity()
{
this._ID = Guid.NewGuid();
}
public BaseEntity(Guid guid)
{
this._ID = guid;
}
#region 实现INotifyPropertyChanged 接口
public event PropertyChangedEventHandler PropertyChanged;
public void OnChangedProperty(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
/// <summary>
/// 基础树类
/// </summary>
public class BaseEntityTree : INotifyPropertyChanged
{
private int _depti = 1;
/// <summary>
/// 当前节点 在树中的深度
/// </summary>
public int Depti
{
get { return _depti; }
set
{
if (!_depti.Equals(value))
{
_depti = value;
OnChangedProperty("Depti");
}
}
}
private BaseEntity _currentBaseEntity = null;
/// <summary>
/// 当前节点对象
/// </summary>
public BaseEntity CurrentBaseEntity
{
get { return _currentBaseEntity; }
set
{
if (this._currentBaseEntity == null || !this._currentBaseEntity.Equals(value))
{
this._currentBaseEntity = value;
OnChangedProperty("CurrentBaseEntity");
}
}
}
private BaseEntityTree _parentBaseEntityTree;
/// <summary>
/// 当前节点的父节点
/// </summary>
public BaseEntityTree ParentBaseEntityTree
{
get { return _parentBaseEntityTree; }
set
{
if (this._parentBaseEntityTree == null || !this._parentBaseEntityTree.Equals(value))
{
this._parentBaseEntityTree = value;
OnChangedProperty("ParentBaseEntityTree");
}
}
}
/// <summary>
/// 当前节点 同级上节点
/// </summary>
private BaseEntityTree _prevBaseEntityTree;
public BaseEntityTree PrevBaseEntityTree
{
get { return _prevBaseEntityTree; }
set
{
if (this._prevBaseEntityTree == null || !this._prevBaseEntityTree.Equals(value))
{
this._prevBaseEntityTree = value;
OnChangedProperty("PrevBaseEntityTree");
}
}
}
private BaseEntityTree _nextBaseEntityTree;
/// <summary>
/// 当前节点 同级下节点
/// </summary>
public BaseEntityTree NextBaseEntityTree
{
get { return _nextBaseEntityTree; }
set
{
if (this._nextBaseEntityTree == null || !this._nextBaseEntityTree.Equals(value))
{
this._nextBaseEntityT
/// <summary>
/// 基础树类
/// </summary>
public class BaseEntityTree : INotifyPropertyChanged
{
private int _depti = 1;
/// <summary>
/// 当前节点 在树中的深度
/// </summary>
public int Depti
{
get { return _depti; }
set
{
if (!_depti.Equals(value))
{
_depti = value;
OnChangedProperty("Depti");
}
}
}
private BaseEntity _currentBaseEntity = null;
/// <summary>
/// 当前节点对象
/// </summary>
public BaseEntity CurrentBaseEntity
{
get { return _currentBaseEntity; }
set
{
if (this._currentBaseEntity == null || !this._currentBaseEntity.Equals(value))
{
this._currentBaseEntity = value;
OnChangedProperty("CurrentBaseEntity");
}
}
}
private BaseEntityTree _parentBaseEntityTree;
/// <summary>
/// 当前节点的父节点
/// </summary>
public BaseEntityTree ParentBaseEntityTree
{
get { return _parentBaseEntityTree; }
set
{
if (this._parentBaseEntityTree == null || !this._parentBaseEntityTree.Equals(value))
{
this._parentBaseEntityTree = value;
OnChangedProperty("ParentBaseEntityTree");
}
}
}
/// <summary>
/// 当前节点 同级上节点
/// </summary>
private BaseEntityTree _prevBaseEntityTree;
public BaseEntityTree PrevBaseEntityTree
{
get { return _prevBaseEntityTree; }
set
{
if (this._prevBaseEntityTree == null || !this._prevBaseEntityTree.Equals(value))
{
this._prevBaseEntityTree = value;
OnChangedProperty("PrevBaseEntityTree");
}
}
}
private BaseEntityTree _nextBaseEntityTree;
/// <summary>
/// 当前节点 同级下节点
/// </summary>
public BaseEntityTree NextBaseEntityTree
{
get { return _nextBaseEntityTree; }
set
{
if (this._nextBaseEntityTree == null || !this._nextBaseEntityTree.Equals(value))
{
this._nextBaseEntityT