Treeview具有一对多Linq到SQL表

时间:2012-10-19 16:01:10

标签: wpf vb.net linq-to-sql

我有一些处理汽车的桌子:

<Table(Name:="tblManufacturer")> Public Class dbManufacturers

    Private _ManufacturerID As Integer
    <Column(Storage:="_ManufacturerID", DbType:="int IDENTITY NOT NULL", IsPrimaryKey:=True, IsDbGenerated:=True, Name:="ManufacturerID")> _
    Public Property ManufacturerID() As Integer
        Get
            Return Me._ManufacturerID
        End Get
        Set(value As Integer)
            Me._ManufacturerID = value
        End Set
    End Property

    Private _ManufacturerName As String
    <Column(Storage:="_ManufacturerName", DbType:="Varchar(50)", Name:="ManufacturerName")> _
    Public Property ManufacturerName() As String
        Get
            Return Me._ManufacturerName
        End Get
        Set(value As String)
            Me._ManufacturerName = value
        End Set
    End Property

    Private _Models As EntitySet(Of dbModels) = New EntitySet(Of dbModels)
    <Association(Storage:="_Models", DeleteRule:="CASCADE", OtherKey:="ManufacturerID")> _
    Public Property Models As EntitySet(Of dbModels)
        Get
            Return _Models
        End Get
        Set(value As EntitySet(Of dbModels))
            _Models.Assign(value)
        End Set
    End Property

End Class

<Table(Name:="tblModels")> Public Class dbModels

    Private _ModelID As Integer
    <Column(Storage:="_ModelID", DbType:="int IDENTITY NOT NULL", IsPrimaryKey:=True, IsDbGenerated:=True, Name:="ModelID")> _
    Public Property ModelID() As Integer
        Get
            Return Me._ModelID
        End Get
        Set(value As Integer)
            Me._ModelID = value
        End Set
    End Property

    Private _ManufacturerID As Integer
    <Column(Storage:="_ManufacturerID", DbType:="int", Name:="ManufacturerID")> _
    Public Property ManufacturerID() As Integer
        Get
            Return Me._ManufacturerID
        End Get
        Set(value As Integer)
            Me._ManufacturerID = value
        End Set
    End Property

    Private _ModelName As String
    <Column(Storage:="_ModelName", DbType:="Varchar(200)", Name:="ModelName")> _
    Public Property ModelName() As String
        Get
            Return Me._ModelName
        End Get
        Set(value As String)
            Me._ModelName = value
        End Set
    End Property

End Class

XAML:

        <TreeView Name="TreeView1" Margin="3" ItemsSource="{Binding ElementName=ManufacturerInfo, Path=tblManufacturers}">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate>
                    <TextBlock Text="{Binding Path=ManufacturerName}" />
                    <HierarchicalDataTemplate.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Source=Models, Path=ModelName}" /> 
                        </DataTemplate>
                    </HierarchicalDataTemplate.ItemTemplate>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>

背后的代码:

Public Class ManufacturerInfo

    Private LinqDB As New DataContext(My.Settings.dbConnection)
    Property tblManufacturers As Table(Of dbManufacturers) = LinqDB.GetTable(Of dbManufacturers)()

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub ManufacturerInfo_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Me.DataContext = Me
    End Sub

End Class

所以我得到制造商列表,但不是嵌套模型。有没有办法使这项工作,或者我必须改变我的LINQ查询的方式?

我无法找到类似的树视图示例。

1 个答案:

答案 0 :(得分:0)

想出来。
还有一点要了解数据绑定的微妙艺术。

        <TreeView Name="TreeView1" Margin="3" ItemsSource="{Binding ElementName=ManufacturerInfo, Path=tblManufacturers}">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Path=Models}">
                    <TextBlock Text="{Binding Path=ManufacturerName}" />
                    <HierarchicalDataTemplate.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=ModelName}" />
                        </DataTemplate>
                    </HierarchicalDataTemplate.ItemTemplate>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>