TreeView WPF C# отображение содержимого файлов по критерию в виде дерева
Всем привет написал такой код, но почему то не отображается ничего, хотя на пером этапе в _currDirectory лежит 16 обьектов. Хелп
public abstract class NotifyPropertyChanged : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public abstract class TreeNode : NotifyPropertyChanged
{
protected string _nodeName;
protected ObservableCollection<TreeNode> _items;
public string NodeName
{
get => _nodeName;
set
{
_nodeName = value;
OnPropertyChanged();
}
}
public ObservableCollection<TreeNode> Items
{
get => _items;
set
{
_items = value;
OnPropertyChanged();
}
}
}
public class DirectoryNode : TreeNode
{
public DirectoryNode()
{
_items = new System.Collections.ObjectModel.ObservableCollection<TreeNode>();
}
}
public class FileNode : TreeNode
{
public FileNode()
{
_items = new System.Collections.ObjectModel.ObservableCollection<TreeNode>();
}
}
public class Seeker
{
private Thread _thread;
private ManualResetEventSlim _event = new ManualResetEventSlim(false);
private uint _countFound = 0;
private uint _allCountFound = 0;
private Regex _format = new Regex(string.Empty);
private DirectoryNode _startDirectory;
private DirectoryNode _currDirectory;
private Stack<DirectoryNode> _nodes;
public uint Found { get => _countFound; }
public uint AllFound { get => _allCountFound; }
public string Format { set => _format = new Regex(Regex.Escape(value)); }
public string StartDirectory { set => _startDirectory = new DirectoryNode { NodeName = value }; }
public string CurrentDirectory { get => _currDirectory.NodeName; }
public Seeker() { }
public void Seek()
{
Restart();
_thread = new Thread(()
=> Seeking()) { IsBackground = true };
_thread.Start();
}
private void Restart()
{
_nodes = new Stack<DirectoryNode>();
_currDirectory = _startDirectory;
_countFound = 0;
_allCountFound = 0;
}
private void Seeking()
{
_nodes.Push(_startDirectory);
while (CheckStop())
{
_currDirectory = _nodes.Pop();
CheckDirectory();
CheckFiles();
}
}
private void CheckFiles()
{
var currDirInfo = new DirectoryInfo(_currDirectory.NodeName);
foreach (var file in currDirInfo.GetFiles())
{
_allCountFound++;
if (_format.IsMatch(file.Name))
{
_countFound++;
_currDirectory.Items.Add(new FileNode { NodeName = file.Name });
}
}
}
private void CheckDirectory()
{
var currDirInfo = new DirectoryInfo(_currDirectory.NodeName);
foreach (var dir in currDirInfo.GetDirectories())
{
_currDirectory.Items.Add(new DirectoryNode { NodeName = dir.Name });
}
}
private bool CheckStop() => _nodes.Count > 0;
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var seeker = new Seeker();
seeker.Format = "*.txt";
seeker.StartDirectory = @"c:\";
seeker.Seek();
}
}
MainWindow.xaml
<Window x:Class="FileSearch.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:FileSearch"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TreeView ItemsSource="{Binding TreeNodes}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:FileNode}" ItemsSource="{Binding Items}">
<TextBlock Text="{Binding NodeName}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:DirectoryNode}" ItemsSource="{Binding Items}">
<TextBlock Text="{Binding NodeName,StringFormat=[{0}]}" />
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
</Grid>
</Window>