0

我有几张处理汽车的桌子:

<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 查询?

我很难找到类似的树视图示例。

4

1 回答 1

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>
于 2012-10-19T17:29:31.620 回答