wpf 中的 treeview 多重绑定
我想将树视图绑定到这样的类:
public class Folder : Base_FileFolder
{
public Folder()
{
Folders = new ObservableCollection<Folder>();
Files = new ObservableCollection<File>();
}
public ObservableCollection<Folder> Folders { get; set; }
public ObservableCollection<File> Files { get; set; }
}
其他类是:
public class File : Base_FileFolder
{
}
public class Base_FileFolder : DependencyObject
{
public string Name
{
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(Base_FileFolder), new UIPropertyMetadata(""));
}
如何创建一个显示文件和文件夹集合的树视图
我想使用这样的东西:
<HierarchicalDataTemplate
DataType="{x:Type model:Folder}"
ItemsSource="{Binding Childs}">
<DockPanel>
<Label Content="{Binding Name}"/> </DockPanel>
</HierarchicalDataTemplate>
所以我得到这样的东西:
rootFolder
|
|-File
|-File
|-Folder
|-File
|-File
|-Folder
|-File
I want to bind a treeview to a class like this one:
public class Folder : Base_FileFolder
{
public Folder()
{
Folders = new ObservableCollection<Folder>();
Files = new ObservableCollection<File>();
}
public ObservableCollection<Folder> Folders { get; set; }
public ObservableCollection<File> Files { get; set; }
}
the other classes ares:
public class File : Base_FileFolder
{
}
public class Base_FileFolder : DependencyObject
{
public string Name
{
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(Base_FileFolder), new UIPropertyMetadata(""));
}
How can I create a treeview that shows Files and Folders collection
I want to use something like this:
<HierarchicalDataTemplate
DataType="{x:Type model:Folder}"
ItemsSource="{Binding Childs}">
<DockPanel>
<Label Content="{Binding Name}"/> </DockPanel>
</HierarchicalDataTemplate>
so I get Somethign like this:
rootFolder
|
|-File
|-File
|-Folder
|-File
|-File
|-Folder
|-File
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
考虑到你的星座,这很容易。
第一:调整课程。您不需要为文件夹类中的文件和文件夹提供两个单独的列表。只需在 Base_FileFolder 类(良好的 OOP)中使用一个
IList
并将其命名为 Children!那么您只需要再执行两个步骤:
两个 HierarchicalDataTemplates
和一个像这样的 TreeView
就是这样。 WPF 的优势在于其简单性。
This is quite easy, considering your constellation.
First: Adjust your classes. You do not need two separate Lists for files and folders in the folders class. Just use one
IList<Base_FileFolder>
inside the Base_FileFolder class (good OOP) and call it Children!Then you'll need only two more steps:
Two HierarchicalDataTemplates
And a TreeView like this
That's it. WPF's strength is its simplicity.
你需要使用
您需要三件事:
You need to use
You'll need 3 things:
你的问题到底是什么?如何将它们结合起来?
CompositeCollection
。编辑:正如评论中提到的,我的 Intuipic 应用程序执行的操作与您的请求非常相似。这是屏幕截图:
What exactly is your question? How to combine them?
CompositeCollection
.EDIT: as mentioned in the comments, my Intuipic application does something very similar to what you're requesting. Here's a screenshot: