I
I
INTERNALINTERFERENCE2021-12-16 12:46:27
WPF
INTERNALINTERFERENCE, 2021-12-16 12:46:27

How to display prefix tree on UI?

$SYS/broker/started, $SYS/broker/clients/countthe logic is as follows: an event of the form , arrives in the logger $SYS/broker/subscriptions/maximum, which is processed by the algorithm to build a prefix tree, and then, logically, a tree structure should appear on ui, but I don’t understand how to display it. I looked at an example of building a tree (

<TreeView Items="{Binding Items}" >
      <TreeView.ItemTemplate>
        <TreeDataTemplate ItemsSource="{Binding Items}">
          <TextBlock Text="{Binding Name}"/>
        </TreeDataTemplate>
      </TreeView.ItemTemplate>
    </TreeView>

), but it is not clear to me how to take out the elements that need to be bound from my code. Here is the tree building algorithm:
public class DrawTreeService
{
    public class Tree
    {
         public readonly Dictionary<string, Tree> Trees = new();
         public readonly HashSet<string> Files = new();
    }

    static private bool DrawTree(Tree rootTree, IEnumerable<string> items)
    {
         var u = false;
         foreach (var filename in items
                      .Where(x => !x.Contains('/'))
                      .Where(x => !rootTree.Files.Contains(x)))
         {
              rootTree.Files.Add(filename);
              u = true;
         }

         var subfolders = items
             .Where(x => x.Contains('/'))
             .Select(filename =>
             {
                  var array = filename.Split('/');
                  var prefix = array.First();
                  var subFile = array.Skip(1).Aggregate((a, b) => a + '/' + b);

                  return (prefix, filename: subFile);
              })
              .ToArray();

          subfolders
              .GroupBy(t => t.prefix)
              .ToList()
              .ForEach(t =>
              {
                  var folder = t.Key;
                  if (!rootTree.Trees.ContainsKey(folder))
                  {
                       rootTree.Trees[folder] = new Tree();
                       u = true;
                  }

                  var u1 = DrawTree(rootTree.Trees[folder], t.Select(t1 => t1.filename).ToArray());
                        u = u1;
              });

                return u;
     }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question