VB.net 中的 WPF TreeView

标签 wpf database vb.net treeview

过去几天我一直在寻找一些工作代码,以便在我的 WPF 应用程序中设置 TreeView 来显示一些数据库数据。我使用 VB,因为我对它更有信心——这极大地限制了那里的代码示例。我发现更多的例子使用手动输入的数据来填充每个节点,而不是数据库,这没什么帮助。 我终于找到了一些代码,将每个节点绑定(bind)到一个查询并通过 DataRelation 关联查询。太棒了,这应该很完美!

该示例使用了 Northwind 数据库,因此我将其更改为我的数据库并插入了查询。令我震惊的是, TreeView 填充正常,除了......所有数据都是不可见的。

代码如下:

Imports MySql.Data.MySqlClient
Imports System.Data
Imports System.ComponentModel
Imports System.Xml
Imports System.IO
Class Window2
Dim connStr As String = "Server=127.0.0.1;Database=psdb;Uid=root;Pwd=;Connect    Timeout=30;"
Dim conn As New MySqlConnection(connStr)
Function GetRelationalData() As DataSet

    Dim CategoryAdapter As MySqlDataAdapter = New MySqlDataAdapter("select distinct Dist_name, dist_id from distributors".ToString, conn)
    Dim ProductsAdapter  As MySqlDataAdapter = New MySqlDataAdapter("select d.dist_id, t.Title_name, title_id from titles t, distributors d where d.dist_id = t.dist_id".ToString, conn)
    Dim ProductData As DataSet = New DataSet()
    CategoryAdapter.Fill(ProductData, "Categories") 'fill Categories
    ProductsAdapter.Fill(ProductData, "Products") 'fill products
    Dim CategoryRelation As DataRelation
    CategoryRelation = New DataRelation("ChildrenRelationship", _
    ProductData.Tables("Categories").Columns("dist_id"), _
    ProductData.Tables("Products").Columns("dist_id"), True)
    CategoryRelation.Nested = True
    ProductData.Relations.Add(CategoryRelation)
    Return ProductData
End Function
Private Sub Button1_Click(ByVal sender As System.Object, _
                          ByVal e As RoutedEventArgs) Handles Button1.Click
    BindData()
End Sub
Private Sub BindData()
    TreeView1.DataContext = GetRelationalData()
End Sub

End Class

和 XAML:

<Window x:Class="Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="400" Width="550"><Window.Resources>
<DataTemplate x:Key="ProductTemplate" >
    <TextBlock Text="{Binding ProductName}"></TextBlock>
</DataTemplate>
<HierarchicalDataTemplate x:Key="CategoryTemplate" 
                  ItemsSource="{Binding ChildrenRelationship}" 
                  ItemTemplate="{StaticResource ProductTemplate}">
    <TextBlock Text="{Binding CategoryName}"></TextBlock>
</HierarchicalDataTemplate>
</Window.Resources>
<Grid>

在这里,我最终得到的数据似乎就在那里,但你看不到。帮助!

enter image description here

PS 对象可能有些困惑。当我发现问题只是用我自己的查询替换查询时,我回去试图尽可能接近原始代码 - 原始代码使用“类别”和“产品”,而我“使用分销商”和“标题”

最佳答案

我强烈建议获得 Snoop并在您的应用程序运行时运行它以查看您的 UI 元素实际绑定(bind)到什么。

我怀疑这只是您的 TextBlock 中存在无效 DataBinding 的问题

关于VB.net 中的 WPF TreeView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8140004/

相关文章:

c# - 如何使用 .NET 更新系统的日期和/或时间

c# - 什么时候在WPF中引发CollectionViewSource.Filter事件?

mysql - 在 1 个查询中连接多个表导致重复

php - 将 jQuery 保存到数据库?

php - 如果条件为真,安全 MYSQL 插入多行

wpf - 简单的网格布局问题

wpf - Metro 风格 (WinRT) UI 是否适合企业应用程序?

WPF弹出隐藏问题

c# - 仅当数据更改时才允许用户保存的更好方法?

vb.net - 在 vb.net 中制作 PC 特定哈希