0

ディレクトリ構造に似た階層データを表示する XamDataGrid があります (フォルダーは内部にフォルダーとファイルを持つことができます)。検索文字列を入力するグリッドの外に別の TextBox があり、TextChanged で RecordFilters を FieldLayouts に適用します。

要件は、検索文字列に一致するファイルがある場合、その親フォルダー (ルートまで) も表示される必要があることです。ただし、他のファイルは表示されません。

ディレクトリとその子ディレクトリおよび子ファイルが検索文字列と一致しない場合、それらはすべて非表示にする必要があります。

ディレクトリが検索文字列と一致し、その子ディレクトリとファイルが一致しない場合、そのベース ディレクトリのみが表示されます。

親ディレクトリが検索に一致しない場合、子がフィールドに一致しても非表示になります。

これに取り組むための最良のアプローチは何ですか?

4

1 に答える 1

1

再帰と組み合わせたLinqから始めることができます。

何かのようなもの:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using rekie;

namespace System.Collections.Generic
{
  public static class FilterExtension
  {
    private static string _criteria;

    public static IList<Node> FilterBy(this IList<Node> source, string criteria)
    {
      _criteria = criteria;

      var copySource =
        (from n in source
         select n).ToList();

      foreach (var node in source)
      {
        if (node.Offspring != null)
        {
          FilterRecursion(node);
        }
      }

      return copySource;
    }

    private static void FilterRecursion(Node parent)
    {
      foreach (var node in parent.Offspring)
      {
        if (node.Offspring != null)
        {
          FilterRecursion(node);
        }

        node.Visible = node.Text.Contains(_criteria);
      }

      parent.Visible = parent.Text.Contains(_criteria) || parent.Offspring.Where(o => o.Visible).Count() > 0;
    }
  }
}

namespace rekie
{


  class Program
  {
    static void Main(string[] args)
    {
      var orig = Node.GetSome();
      var Results = orig.FilterBy("O.o");
    }
  }

  public class Node
  {
    public string Text { get; set; }
    public IList<Node> Offspring { get; set; }
    public bool Visible { get; set; }

    public static IList<Node> GetSome()
    {
      return
        new List<Node>()
        { 
          new Node()
          { 
            Text="Chidori", 
            Offspring=new List<Node>()
              { 
                new Node(){ Text="Rasengan "}
              }
          }, 
          new Node()
          { 
            Text="Kage Shuriken no Jutsu", 
            Offspring=new List<Node>()
            {
              new Node(){Text="Amagumo O.o"}
            }
          }, 
          new Node()
          { 
            Text="Kage Bunshin no Jutsu", 
            Offspring=new List<Node>()
          }, 
          new Node()
          { 
            Text="Oiroke no Jutsu", 
            Offspring=new List<Node>()
            {
              new Node(){ Text="O.o"}
            }
          }, 
          new Node()
          { 
            Text="Ranshinsho O.o", 
            Offspring=new List<Node>()
            {
              new Node(){ Text="Shikotsumyaku" },
              new Node(){ Text="Byakugan"}
            }
          }
        };
    }
  }
}
于 2012-09-28T11:31:32.020 に答える