c# - 文件浏览器 TreeView MVVM

标签 c# wpf mvvm tree

大家好,我的最终目标是使用 WPF 中的 TreeView 创建一个可选择的文件浏览器。我让 TreeView 正常显示,然后我决定转到正确的 MVVM 结构, TreeView 将不会显示。我做了一些研究,发现我需要使用 HierarchicalDataTemplates 和绑定(bind)来使其工作。我只是对如何使用 HierarchialDataTemplate(子级)返回复杂目录的递归函数感到困惑。

我目前使用 TreeView 类型作为 VM 中 TreeView 的顶级容器。下面是我的 ViewModel,它具有遍历目录的递归函数。 (如果有更简单的方法可以随时告诉我,这是我的第一次尝试:))。

        public void onBrowseCommand ( object commandInvoker )
    {
        Microsoft.Win32.OpenFileDialog win = new Microsoft.Win32.OpenFileDialog();

        Nullable<bool> result = win.ShowDialog();

        if (result == true)
        {
            string filename = win.FileName;
            rootfile= filename;

            rootfile = filename;
            int index = rootfile.Length;

            index = index - 4;
            rootfile = rootfile.Remove(index);



            //Get file version for text box
            var fversion = FileVersionInfo.GetVersionInfo(filename);
            version = fversion.FileVersion;



            //get date created 
            DateTime fdate = File.GetCreationTime(filename);
            date = fdate.ToString();

            //Display Folder Contents
            showzip(filename);
            Window_Loaded();
        }
    }
    private void showzip(string path)
    {
        try
        {
            bool isZip = path.Contains(".zip");

            if (isZip)
            {
                dynamic shellApplication = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));

                dynamic compressedFolderContents = shellApplication.NameSpace(path).Items;

                string destinationPath = System.IO.Directory.GetParent(path).FullName;

                dynamic destinationFolder = shellApplication.NameSpace(destinationPath);

                destinationFolder.CopyHere(compressedFolderContents);
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void Window_Loaded()
    {
        string dir = rootfile;

        TreeViewItem items = new TreeViewItem();
        items.Header = dir.Substring(dir.LastIndexOf('\\') + 1);
        items.Tag = dir;
        items.FontWeight = FontWeights.Normal;
        fillFiles(items, dir);

        foreach (string s in Directory.EnumerateDirectories(dir))
        {
            TreeViewItem item = new TreeViewItem();
            item.Header = s.Substring(s.LastIndexOf('\\') + 1);
            item.Tag = s;
            item.FontWeight = FontWeights.Normal;
            fillFiles(item, s);
            FillTreeView(item, s);
            items.Items.Add(item);
        }

        foldersItem.Items.Add(items);
    }

    private void FillTreeView(TreeViewItem parentItem, string path)
    {
        foreach (string str in Directory.EnumerateDirectories(path))
        {
            TreeViewItem item = new TreeViewItem();
            item.Header = str.Substring(str.LastIndexOf('\\') + 1);
            item.Tag = str;
            item.FontWeight = FontWeights.Normal;
            parentItem.Items.Add(item);
            fillFiles(item, str);
            FillTreeView(item, str);
        }


    }

    private void fillFiles(TreeViewItem parentItem, string path)
    {
        foreach (string str in Directory.EnumerateFiles(path))
        {
            TreeViewItem item = new TreeViewItem();
            item.Header = str.Substring(str.LastIndexOf('\\') + 1);
            item.Tag = str;
            item.FontWeight = FontWeights.Normal;
            parentItem.Items.Add(item);

        }
    }

这也是我的 XAML。谢谢,如果不是在正确的方向上推,只需要推。
        <TreeView DockPanel.Dock="Left" Margin="5,0,5,5" x:Name="foldersItem" ItemsSource="{Binding foldersItem}" >

        </TreeView>

最佳答案

您需要添加一些数据模板,是的。

你会做类似的事情

  <TreeView.Resources>
      <HierarchicalDataTemplate DataType="{x:Type models:MyFolderModel}" ItemsSource="{Binding Files}">
          <TextBlock Text="{Binding Name}" />
      </HierarchicalDataTemplate>

      <HierarchicalDataTemplate DataType="{x:Type models:MyFileModel}">
          <TextBlock Text="{Binding Name}" />
      </HierarchicalDataTemplate>
  </TreeView.Resources>

这是假设您的结构看起来像...
  public class MyFolderModel
  {
      public string Name { get; set; }
      public IEnumerable<MyFileModel> Files { get; set; }
  }

  public class MyFileModel
  {
      public string Name { get; set; }
  }

当然,还有更多的工作要做,但希望能帮助您入门。

关于c# - 文件浏览器 TreeView MVVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25555206/

相关文章:

c# - ASP.NET MVC - 如何限制分页元素的数量并在记录较多时使用 '...'(三个点)进行过滤

c# - 具有设置间隔的 Ajax 或 SignalR

c# - 带 Sympy 的 IronPython 脚本

c# - WPF:绑定(bind)到 MainWindow 属性

c# - 如何从UserControl1 WPF MVVM中的按钮将数据从一个UserControl1传递到UserControl2

c# - 使用 Enumerable 和 Lambda 过滤文件列表并删除不需要的扩展名

c# - 如何加载更快的 WPF 窗口?

c# - 如何使用 Postsharp 捕捉属性变化?

silverlight - Silverlight MVVM隔离存储

c# - 附加行为的继承