【发布时间】:2016-08-13 05:31:09
【问题描述】:
我正在制作一个过滤项目类,它将在 WPF 中显示为树视图。 filteritems 类仅包含 treeitems 类中包含特定条件的特定节点项。我能够克隆所有树项并将它们添加到过滤项列表中。从那里我找到不符合标准的节点并适当地删除它们。但是我发现使用克隆使我无法删除这些项目。关于克隆物品以及为什么它们不能从我的收藏中删除,我应该知道些什么?
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>();
}
}
【问题讨论】:
-
你为什么要克隆它们?
-
请输入您的代码并澄清您的问题
-
我正在克隆它们,因为 TreeItems 和 FilterTreeItems 类都是指针,并且由于这些类的创建方式,删除节点是过滤树的最简单方法。但是,除了 TakeOutTrash 方法之外,所有逻辑都适用于过滤,我实际上无法删除(节点)。即使我的代码达到了这个方法。