0

这是我的代码,用于创建具有多个级别并显示复选框的 Treeview。我无法使用以下代码显示文件夹下的文件。导致此问题的 Xaml 有什么问题?还是做不到?

Window1.xaml

<Window x:Class="TreeViewSample.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TreeViewSample"
    Title="Window1" Height="500" Width="800">
<DockPanel>
    <TreeView DockPanel.Dock="Left" Width="300" ItemsSource="{Binding Folders}">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type local:Folder}" ItemsSource="{Binding Folders}">
                <StackPanel Orientation="Horizontal">
                    <CheckBox Margin="2" />
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="{x:Type local:File}" ItemsSource="{Binding Files}">
                <StackPanel Orientation="Horizontal">
                    <CheckBox Margin="2" />
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </HierarchicalDataTemplate>
        </TreeView.Resources>
    </TreeView>
    <Grid></Grid>
</DockPanel>

Window1.xaml.cs 代码隐藏构造函数

this.DataContext = new Window1ViewModel();

Window1ViewModel

public class Window1ViewModel : INotifyPropertyChanged
{
    public Window1ViewModel()
    {
        _folders = new ObservableCollection<Folder>(Sample.GetFoldersAndFiles());
    }

    private ObservableCollection<Folder> _folders;

    public ObservableCollection<Folder> Folders
    {
        get { return _folders; }
        set { _folders = value; OnPropertyChanged(); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName]string propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}

示例数据和类

    public class Sample
{
    public static List<Folder> GetFoldersAndFiles() 
    {

        var folders = new List<Folder>();

        Folder folder1 = new Folder { Name ="Folder 1" };
        Folder folder2 = new Folder { Name = "Folder 2" };
        Folder folder3 = new Folder { Name = "Folder 3" };
        Folder folder11 = new Folder { Name = "Folder 11" };
        Folder folder12 = new Folder { Name = "Folder 12" };
        Folder folder22 = new Folder { Name = "Folder 22" };

        folder1.Folders.Add(folder11);
        folder1.Folders.Add(folder12);
        folder2.Folders.Add(folder22);

        File file1 = new File { Name="File 1" };
        File file2 = new File { Name = "File 2" };
        File file3 = new File { Name = "File 3" };
        File file4 = new File { Name = "File 4" };
        File file11 = new File { Name = "File 11" };
        File file31 = new File { Name = "File 31" };

        file1.Files.Add(file11);
        file3.Files.Add(file31);

        folder1.Files.Add(file1);
        folder1.Files.Add(file2);
        folder2.Files.Add(file3);
        folder3.Files.Add(file4);

        folders.Add(folder1);
        folders.Add(folder2);
        folders.Add(folder3);


        return folders;

    }
}


public class Folder
{
    public Folder() 
    {
        Folders = new List<Folder>();
        Files = new List<File>();
    }

    public string Name { get; set; }
    public ICollection<Folder> Folders { get; set; }
    public ICollection<File> Files { get; set; }

}

public class File
{
    public File()
    {
        Files = new List<File>();
    }

    public string Name { get; set; }
    public Folder Folder { get; set; }
    public ICollection<File> Files { get; set; } // This groups files together under a single file.
}

我期望树如下:

  • 文件夹 1

    • 文件夹 11
    • 文件夹 12
    • 文件 1
    • 文件2
  • 文件夹 2

    • 文件夹22
    • 文件3等,
4

2 回答 2

2

您可以使用CompositeCollection( msdn )。

首先,您应该添加到Folder将返回的类属性CompositeCollection

public class Folder
{
    public Folder()
    {
        Folders = new List<Folder>();
        Files = new List<File>();
    }

    public string Name { get; set; }
    public ICollection<Folder> Folders { get; set; }
    public ICollection<File> Files { get; set; }

    public IEnumerable Items
    {
        get
        {
            var items = new CompositeCollection();
            items.Add(new CollectionContainer { Collection = Folders });
            items.Add(new CollectionContainer { Collection = Files });
            return items;
        }
    }
}

第一步后,您应该修改HierarchicalDataTemplate如下:

<HierarchicalDataTemplate DataType="{x:Type local:Folder}" ItemsSource="{Binding Items}">
    <StackPanel Orientation="Horizontal">
        <CheckBox Margin="2" />
        <TextBlock Text="{Binding Name}" />
    </StackPanel>
</HierarchicalDataTemplate>                
<HierarchicalDataTemplate DataType="{x:Type local:File}">
    <StackPanel Orientation="Horizontal">
        <CheckBox Margin="2" />
        <TextBlock Text="{Binding Name}" />
    </StackPanel>
</HierarchicalDataTemplate>
于 2013-05-21T17:32:02.500 回答
-1

得到它的工作。以下是我所做的更改。

        public IList Children
    {
        get
        {
            return new CompositeCollection()
        {
            new CollectionContainer() { Collection = Folders },
            new CollectionContainer() { Collection = Files }
        };
        }
    }

                <HierarchicalDataTemplate DataType="{x:Type local:Folder}" ItemsSource="{Binding Children}">

这是给我解决方案的链接:

具有混合类型的 WPF Treeview 数据绑定分层数据

于 2013-05-21T17:34:16.293 回答