DataSet中的分层TreeView

时间:2017-01-23 14:25:56

标签: wpf data-binding treeview

我是WPF的新手,无法获得如何将该数据集绑定到treeview

Dim dtHDD As New DataTable("Disks")
dtHDD.Columns.Add("ID")
dtHDD.Columns.Add("DiskName")

dtHDD.Rows.Add(1, "Disc1")
dtHDD.Rows.Add(2, "Disc2")

Dim dtFolders As New DataTable("Folders")
dtFolders.Columns.Add("ID")
dtFolders.Columns.Add("DiskID")
dtFolders.Columns.Add("FolderID")
dtFolders.Columns.Add("FolderName")

dtFolders.Rows.Add(1, 2, DBNull.Value, "Music")
dtFolders.Rows.Add(2, 1, DBNull.Value, "Documents")
dtFolders.Rows.Add(3, 1, 2, "Scatches")
dtFolders.Rows.Add(4, 1, 3, "Images")
dtFolders.Rows.Add(5, 1, 3, "Folder1")
dtFolders.Rows.Add(6, 1, 2, "Bin")
dtFolders.Rows.Add(7, 2, DBNull.Value, "Moves")
dtFolders.Rows.Add(8, 1, 5, "Another one")


Dim dtFiles As New DataTable("Files")
dtFiles.Columns.Add("ID")
dtFiles.Columns.Add("FolderID")
dtFiles.Columns.Add("FileName")

dtFiles.Rows.Add(1, 8, "file1")
dtFiles.Rows.Add(2, 2, "file2")
dtFiles.Rows.Add(3, 3, "file3")
dtFiles.Rows.Add(4, 2, "file4")
dtFiles.Rows.Add(5, 5, "file5")
dtFiles.Rows.Add(6, 5, "file6")
dtFiles.Rows.Add(7, 8, "file7")
dtFiles.Rows.Add(8, 8, "file8")
dtFiles.Rows.Add(9, 1, "file9")

Dim ds As New DataSet
ds.Tables.Add(dtHDD)
ds.Tables.Add(dtFolders)
ds.Tables.Add(dtFiles)
ds.Relations.Add("DiskFolder", ds.Tables("Disks").Columns("ID"), ds.Tables("Folders").Columns("DiskID"))
ds.Relations.Add("FolderFolder", ds.Tables("Folders").Columns("ID"), ds.Tables("Folders").Columns("FolderID"))
ds.Relations.Add("FolderFile", ds.Tables("Folders").Columns("ID"), ds.Tables("Files").Columns("FolderID"))

treeTest.ItemsSource = ds.Tables("Disks").DefaultView

因此每个磁盘只能包含文件夹。 每个文件夹可以包含文件和文件夹。 我做了以下标记

<TreeView x:Name="treeTest" Margin="380,0,0,0">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding DiskFolder}">
            <TextBlock Text="{Binding DiskName}"/>
            <HierarchicalDataTemplate.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding FolderName}" />
                </DataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

但我不知道如何为Folder表设置RowFilter规则以及如何在该树视图中添加文件。

3 个答案:

答案 0 :(得分:0)

您可以使用DataTemplateSelector根据父表的名称选择适当的模板:

Public Class TemplateSelector
    Inherits DataTemplateSelector

    Public Property DiskTemplate As DataTemplate
    Public Property FolderTemplate As DataTemplate
    Public Property FileTemplate As DataTemplate

    Public Overrides Function SelectTemplate(item As Object, container As DependencyObject) As DataTemplate

        Dim drv = CType(item, System.Data.DataRowView)
        Select Case drv.DataView.Table.TableName
            Case "Disks"
                Return DiskTemplate
            Case "Folders"
                Return FolderTemplate
            Case "Files"
                Return FileTemplate
        End Select

        Return Nothing

    End Function

End Class

用法:

<Window x:Class="Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <HierarchicalDataTemplate x:Key="diskTemplate" ItemsSource="{Binding DiskFolder}">
            <TextBlock Text="{Binding DiskName}"/>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate x:Key="folderTemplate" ItemsSource="{Binding FolderFile}">
            <TextBlock Text="{Binding FolderName}"/>
        </HierarchicalDataTemplate>

        <DataTemplate x:Key="fileTemplate">
            <TextBlock Text="{Binding FileName}"/>
        </DataTemplate>

        <local:TemplateSelector x:Key="TemplateSelector"
                                DiskTemplate="{StaticResource diskTemplate}" 
                                FolderTemplate="{StaticResource folderTemplate}"
                                FileTemplate="{StaticResource fileTemplate}"/>
    </Window.Resources>
    <StackPanel>
        <TreeView x:Name="treeTest" ItemTemplateSelector="{StaticResource TemplateSelector}" />
    </StackPanel>
</Window>

enter image description here

答案 1 :(得分:0)

我这样做了

<HierarchicalDataTemplate x:Key="folderTemplate" ItemsSource="{Binding FolderFolder}">
    <TextBlock Text="{Binding FolderName}"/>
</HierarchicalDataTemplate>

MyWindow

我认为左边为行添加过滤器

RowFilter = "FolderID is null"

但怎么不明白......

答案 2 :(得分:0)

最后我希望看到那个结构

<disc Name="Disc1">
    <folder Name="Documents">
        <folder Name="Scatches">
            <folder Name="Images"/>
            <folder Name="Folder1">
                <folder Name="Another one">
                    <file Name="file1"/>
                    <file Name="file7"/>
                    <file Name="file8"/>
                </folder>
                <file Name="file5"/>
                <file Name="file6"/>
            </folder>
            <file Name="file3"/>
        </folder>
        <folder Name="Bin"/>
        <file Name="file2"/>
        <file Name="file4"/>
    </folder>
</disc>
<disc Name="Disc2">
    <folder Name="Music">
        <file Name="file9"/>
    </folder>
    <folder Name="Moves" />
</disc>