在WPF中使用ObservableCollection集合操作TreeView节点

          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 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

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值