从列表中删除克隆的节点

时间:2016-04-20 13:31:36

标签: c# clone deep-copy

我正在制作一个filteritems类,它将在WPF中显示为树视图。 filtereditems类仅包含来自包含特定条件的treeitems类的某些节点项。我能够克隆所有树项并将它们添加到filtereditems列表中。从那里我发现不符合标准的节点并适当地删除它们。但是我发现使用克隆使我无法删除这些项目。我是否应该知道克隆物品以及为什么它们不能从我的藏品中删除?

public class Node: INotifyPropertyChanged, ICloneable
{
    public Name { get;set;}
    public ID {get;set;}
    public ParentNode {get;set;}
    public ObservableCollection<Nodes> ChildNodes{get;set;}
    public object Clone() 
        {
            Node toReturn = new Node();
            toReturn.Name = this.Name;
            toReturn.ID = this.ID;
            toReturn.ParentNode = this.ParentNode;
            foreach (Node child in this.ChildNodes)
            {
                toReturn.ChildNodes.Add((Node) child.Clone());
            }
            return toReturn;
        }
 }

 public void filterStart(ChildNodesListViewDataSource _filterStart)
        {
            if (this.FilterString != null && this.Entity != null)
            {
                this.TotalItemsNumber = 0;
                this.FilterItemsNumber = 0;

                    this.FilterTreeItems.Clear();
                    foreach (Node y in TreeItems)
                    {

                        this.FilterTreeItems.Add((Node)y.Clone());
                        foreach (Node x in FilterTreeItems)
                        {
                            FilterRoot(x);
                        }
                    }

                TakeOutTrash();

public bool FilterRoot(Node FilterItems)
        {
            bool HasMatchingChildren = false;
            if (FilterItems.ChildNodes != null ||        FilterItems.ChildNodes.Count !=0)
            {
                foreach (Node FilterItemsComponenents in FilterItems.ChildNodes)
                {
                    if (FilterRoot(FilterItemsComponenents))
                    {
                        HasMatchingChildren = true;
                    }
                }
            }
            string NameOfFilterItem = FilterItems.Name.ToUpper();
            string FilterStringUpperCase = FilterString.ToUpper();
            bool FilterStringCheck = NameOfFilterItem.Contains(FilterStringUpperCase);
            if (!FilterStringCheck && !HasMatchingChildren)
            {
                trimIDs.TrashCan.Add(FilterItems);
                return false;
            }
            else
            {
                return true;
            }
        }

public void TakeOutTrash()
        {
            foreach (Node node in trimIDs.TrashCan)
            {
                this.FilterTreeItems.Remove(node);
            }
        }

        public class TrimIDs
        {
            public IList<ComponentNodeViewModel> TrashCan { get; set;}
            {
                TrashCan = new List<ComponentNodeViewModel>();
            }
        }

1 个答案:

答案 0 :(得分:0)

我实际上是通过创建另一个克隆方法解决了我的问题,该方法克隆了不在克隆节点列表中的任何节点:(希望这可以帮助处于同样困境中的任何人)。

public object Clone(IList<Node> ListNotToClone)
    {
        NodetoReturnFiltered = new Node();
        toReturnFiltered.Name = this.Name;
        toReturnFiltered.ID = this.ID;
        toReturnFiltered.ParentNode= this.ParentNode;
        foreach (Node child in this.ComponentNodes)
        {
            if (!ListNotToClone.Contains(child))
            {
                toReturnFiltered.ComponentNodes.Add((Node)child.Clone(ListNotToClone));
            }
        }
        return toReturnFiltered;
    }

然后我使用了这个方法:

public void TakeOutTrash()
    {
        foreach (ComponentNodeViewModel root in this.FilterTreeItems)
        {
            FilterHolder = (ComponentNodeViewModel)root.Clone(trimIDs.TrashCan);
        }
    }

并将FilterHolder传递回FilterStart方法中的FilterTreeItems,

this.FilterTreeItems.Clear();
this.FilterTreeItems.Add(FilterHolder);