0

I'm looking for a way to hides node in a tree. I'm filtering a node with a node type, this is my original structure:

root
 +FolderA
    -File1
    -File2
    -File3
 +FolderB
    -File4
    -File5
    +FolderB-1
            -File6

This is what I'm trying to do:

+root
 -File1
 -File2
 -File3
 -File4
 -File5
 -File6

I have created my class XNode with a type and a list of childrens. (this is pseudo language for abbreviation)

Class XNode
  MyType type;
  string Name;
  List<XNode> childrens; 
End Class

I have created a factory class that query my database and create original tree.

I have created a Xaml treeView UserControl to bind my root XNode created.

Via Xaml is not possible to hide a Node, this operation must be done with binded object (my root Xnode created).

Now my question is: There are recursive alghoritms to cancel a "Folder type" node, get its childrens and add them to parent Node?

I'm try:

  public XNode RemoveFoldersElements(ref XNode rootNode)
    {

        if (rootNode != null)
        {
            if (rootNode.Children.Count > 0)
            {
                for (int i = 0; i < rootNode.Children.Count; i++)
                {
                    XNode children = rootNode.Children.ElementAt(i);
                    if (children.WType == NodeType.Type_FOLDER)
                    {
                        XNode tempNode = RemoveFoldersElements(ref children);
                        if (tempNode != null)
                            rootNode.Children.Add(tempNode);
                        rootNode.Children.RemoveAt(i);
                    }
                    else
                    {
                        RemoveFoldersElements(ref children);
                    }

                }
            }

        }
        return null;
    }

but not success because not all nodes of Type_FOLDER are erased (it jump one level) ! Any Idea?

4

1 に答える 1

0

OK、アイデアは次のとおりです。フィルタリング中にツリーを平坦化します。

public static IEnumerable<TSource> Flatten<TSource>(this IEnumerable<TSource> source, Func<TSource, IEnumerable<TSource>> childCollectionSelector, Func<TSource, bool> predicate)
{
    foreach (var item in source)
    {
        if(predicate(item))
            yield return item;
        foreach (var subitem in childCollectionSelector(item).Flatten(childCollectionSelector, predicate).Where(predicate))
        {
            yield return subitem;
        }
    }
}

次のように使用できます。

var rootList = new []{rootElement}; // Flattening works on the collection
var files = rootList.Flatten(x => x.children, x => x.type == MyType.FILE);

そして、循環参照がなく、すべてのコレクションが初期化されていれば、機能します。循環依存関係がある場合は、StackOverflowException...に挨拶します。

于 2013-01-16T16:02:07.357 に答える