wpf 中的 treeview 多重绑定

发布于 2024-09-15 07:05:09 字数 1237 浏览 7 评论 0原文

我想将树视图绑定到这样的类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽梦忆笙歌 2024-09-22 07:05:10

考虑到你的星座,这很容易。

第一:调整课程。您不需要为文件夹类中的文件和文件夹提供两个单独的列表。只需在 Base_FileFolder 类(良好的 OOP)中使用一个 IList 并将其命名为 Children!

那么您只需要再执行两个步骤:

  1. 两个 HierarchicalDataTemplates

    
        <网格>
            
        
    
    
    
        <网格>
            
        
    
    
  2. 和一个像这样的 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:

  1. Two HierarchicalDataTemplates

    <HierarchicalDataTemplate DataType="{x:Type FolderNode}"  ItemsSource="{Binding Path=Children}">
        <Grid>
            <TextBlock Text="{Binding FolderName}" />
        </Grid>
    </HierarchicalDataTemplate>
    
    <HierarchicalDataTemplate DataType="{x:Type FileNode}"  ItemsSource="{Binding Path=Children}">
        <Grid>
            <TextBlock Text="{Binding FileName}" />
        </Grid>
    </HierarchicalDataTemplate>
    
  2. And a TreeView like this

    <TreeView Name="TreeViewFileTree" ItemsSource="{rootFolder.Children}" />
    

That's it. WPF's strength is its simplicity.

圈圈圆圆圈圈 2024-09-22 07:05:10

你需要使用
您需要三件事:

  1. 一个 HierarchicalDataTemplate,就像您所拥有的那样,用于执行父级+子级操作,并对文件夹进行模板化。您也许可以在此处使用 CompositeCollection 来合并文件夹+文件,但我不确定...您可能必须向文件夹类添加另一个属性,该属性返回文件和文件夹的并集并将其称为“ Children”或其他什么...
  2. 树中模板文件的 DataTemplate
  3. 告诉树根据树中的项目在模板之间切换的 TemplateSelector。不要在树上设置 ItemTemplate,而是将 ItemTemplateSelector 设置为此。

You need to use
You'll need 3 things:

  1. a HierarchicalDataTemplate, like you have, to do parent+children, and template the folders. you MIGHT be able to use a CompositeCollection here to merge the folders+files, but i'm not sure about that...you might have to add another property to your folder class that returns the union of files and folders and call it "Children" or whatever...
  2. A DataTemplate to template files in the tree
  3. A TemplateSelector to tell the tree to switch between templates depending on the item in the tree. Instead of setting an ItemTemplate on the tree, set the ItemTemplateSelector to this.
葬﹪忆之殇 2024-09-22 07:05:09

你的问题到底是什么?如何将它们结合起来? CompositeCollection

编辑:正如评论中提到的,我的 Intuipic 应用程序执行的操作与您的请求非常相似。这是屏幕截图:

alt text

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:

alt text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文