2

我对 MVVM 和 WPF 比较陌生。我正在尝试用一个目录和它的文件/子目录填充 TreeView 控件(实际上是我解压缩的 zip 文件的内容)

在这个 SO question之后,我有以下课程:

namespace IFR_Full.Model
{
    public class Item
    {
        public string Name { get; set; }
        public string Path { get; set; }
    }

    public class FileItem : Item
    {
    }

    public class DirectoryItem : Item
    {
        public List<Item> Items { get; set; }

        public DirectoryItem()
        {
            Items = new List<Item>();
        }
    }

    public class TVItemProvider
    {
        public List<Item> GetItems(string path)
        {
            var items = new List<Item>();
            var dirInfo = new DirectoryInfo(path);

            foreach (var directory in dirInfo.GetDirectories())
            {
                var item = new DirectoryItem
                    {
                        Name = directory.Name,
                        Path = directory.FullName,
                        Items = GetItems(directory.FullName)
                    };
                items.Add(item);
            }

            foreach (var file in dirInfo.GetFiles())
            {
                var item = new FileItem
                {
                    Name = file.Name,
                    Path = file.FullName
                };
                items.Add(item);
            }
            return items;
        }
    }
}

在我的 ViewModel 类中,我有以下属性:

TVItemProvider TVIP = new TVItemProvider();

private List<Item> _tvitems;
public List<Item> TVItems
{
    get { return _tvitems; }
}

这是在此方法中创建的:

private void LoadIDMLTreeView(string path)
{
    _tvitems = TVIP.GetItems(path);
}

我像这样设置 MainWindow 的标题和 DataContext:

...
xmlns:ViewModel="clr-namespace:IFR_Full"
xmlns:Model ="clr-namespace:IFR_Full.Model"
...

<Window.DataContext>
    <ViewModel:ExcelImportViewModel/>
</Window.DataContext>

并像这样设置我的treeview xaml代码:

<TreeView ItemsSource="{Binding}" Name="IDMLView" Margin="10,171.74,10,8" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type Model:DirectoryItem}" ItemsSource="{Binding Path=TVItems}">
            <TextBlock Text="{Binding Path=Name}" ToolTip="{Binding Path=Path}" />                  
        </HierarchicalDataTemplate> 
        <DataTemplate DataType="{x:Type Model:FileItem}">
     <TextBlock Text="{Binding Path=Name}" ToolTip="{Binding Path=Path}" />
    </DataTemplate>             
 </TreeView.Resources>
</TreeView>

当我在调试模式下运行程序时,我可以看到 TVItems 包含适当的项目(目录和文件),但我的 TreeView 控件是空白的。

我想问题出在绑定上?

4

1 回答 1

2
  • 更改<TreeView ItemsSource="{Binding}" ...<TreeView ItemsSource="{Binding TVItems}" ...
  • 另外,更改为<HierarchicalDataTemplate DataType="{x:Type local:DirectoryItem}" ItemsSource="{Binding Items}" >
  • 你的班级必须是这样的:

     public class TVItemProvider
        {
    
            List<object> items = new List<object>();
              DirectoryInfo dirInfo;
              public List<object> GetItems(string path)
            {
                dirInfo = new DirectoryInfo(path);
                foreach (var directory in dirInfo.GetDirectories())
                {
                    var item = new DirectoryItem
                    {
                        Name = directory.Name,
                        Path = directory.FullName,
                        Items = new TVItemProvider().GetItems(directory.FullName)
                    };
                    items.Add(item);
                }
    
                foreach (var file in dirInfo.GetFiles())
                {
                    var item = new FileItem
                    {
                        Name = file.Name,
                        Path = file.FullName
                    };
    
                    items.Add(item);
                }
                return items;
            }
    

    }

  • 最后将列表的类型更改为List<object>(全部)
    希望它会有所帮助

于 2012-08-28T19:27:11.533 回答