递归获取一个树层次结构(合同清单)的部分选择项(计量合同清单),以及递归删除部分清单

递归获取一个树层次结构(合同清单)的部分选择项(计量合同清单),以及递归删除部分清单,说明如下:

1、递归获取一个树层次结构(合同清单)的部分选择项

//  参数1  EQConList clst  是选择需要加入 集合两者清单项,注意 两者的是不同的,只是 编码、名称、单位、类型相同。

 private EQConSttList addSelectedandParent(EQConList clst,  CollectionSrc sttcollSource)

 {

      //首先从  sttcollSource (加入的集合)中查找,是否存在对应编码与名称的细目。

      EQConSttList curnode = sttcollSource.List.OfType<EQConSttList>().FirstOrDefault<EQConSttList>(slt => slt.Code == clst.Code && slt.Name == clst.Name);

     if ( curnode == null)
    {
           curnode = View.ObjectSpace.CreateObject<EQConSttList>();
           curnode.Type = clst.Type;
           curnode.Code = clst.Code;
          curnode.Name = clst.Name;

         if (clst.Parent != null)

         { //然后增加父
                curnode.Parent = addcurandparent(clst.Parent, curSettle, sttcollSource);
          }               
       }

       return curnode;
 }

 

2、删除选择节点 子节点 以及 没有其它同级 的父节点。

 //动作按钮事件的执行

private void DeleteSttList_Execute(object sender, SimpleActionExecuteEventArgs e)
{

      //表示 选中行所在的集合源  List
      PropertyCollectionSource sttcollSource = (PropertyCollectionSource)((ListView)View).CollectionSource;      

      EQConSettle curSettle = sttcollSource.MasterObject as EQConSettle;            // 分录的表头      

      EQConSttList curlst = (EQConSttList)View.CurrentObject;                               //选中的当前行对象

      EQConSttList parentlst = curlst.Parent;                                                             //选中的当前行对象 的父对象

      if (curlst != null)
      {
           delcur_children(curlst, sttcollSource);                //迭代删除子节点对象
           delparent(parentlst, sttcollSource);                    //迭代删除父节点对象,如果没有其它同级对象
      }
}

 

//迭代删除子节点对象, 参数1 是 选中的节点对象,参数2 是 节点所在集合。

private void delcur_children(EQConSttList mynode, PropertyCollectionSource sttcoll)
{
         HashSet<EQConSttList> childset = new HashSet<EQConSttList>();     //创建一个临时集合,用于保存将要删除的子节点
         foreach (EQConSttList childnode in sttcoll.List)
         {
                 if (mynode.Equals(childnode.Parent))
                 {
                        childset.Add(childnode);
                 }
          }
          foreach (EQConSttList childnode in childset)
          {
                 delcur_children(childnode, sttcoll);
          }
           sttcoll.Remove(mynode);
}

 

//迭代删除父节点对象,如果没有其它同级对象,参数同上

private void delparent(EQConSttList mynode, PropertyCollectionSource sttcoll)
{
       if (mynode == null)  return;
       foreach (EQConSttList node in sttcoll.List)
       {
              if (mynode.Equals(node.Parent)) return;
       }
       EQConSttList parentnode = mynode.Parent;
       sttcoll.Remove(mynode);
       delparent(parentnode, sttcoll);
}

 

posted @ 2017-10-13 09:17  Hopesun  阅读(223)  评论(0编辑  收藏  举报