以下是我创建Treeview的代码,该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.
}
我希望如下所示:
Folder1中
FOLDER2
答案 0 :(得分:1)
您可以使用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>
答案 1 :(得分:0)
搞定了。以下是我所做的改变。
public IList Children
{
get
{
return new CompositeCollection()
{
new CollectionContainer() { Collection = Folders },
new CollectionContainer() { Collection = Files }
};
}
}
<HierarchicalDataTemplate DataType="{x:Type local:Folder}" ItemsSource="{Binding Children}">
这是给我解决方案的链接: