2014-08-28 74 views
0

嗨,大家好,我的最终目标是使用WPF中的树视图创建一个可选择的文件浏览器。我有树视图正常显示,然后我决定移动到适当的MVVM结构,树视图不会显示。我已经做了一些研究,并且看到我需要使用HierarchicalDataTemplates和绑定来使其工作。我只是困惑于如何使用HierarchialDataTemplate(children)返回复杂目录的递归函数。文件浏览器树视图MVVM

我目前在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> 

回答

0

您需要添加一些DataTemplates,是的。

你会做这样的事情

<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; } 
    } 

当然,还有参与更多的工作,但应该有希望帮助你开始。

+0

我只有一个简单的类,它有名称,文件路径和方法。为了这个工作,我需要创建类来包含文件夹和文件? – 2014-08-28 18:18:34

+0

是的,如果你想要做MVVM的话,你需要有代表你的对象的类。 这个链接有几个不同的例子,它可能会帮你清除它: http://www.codeproject.com/Articles/26288/Simplifying-the-WPF-TreeView-by-Using-the- ViewMode – Locke 2014-08-28 18:21:02

+0

我仍然无法看到树显示。 – 2014-08-29 18:45:46