HierarchicalDataTemplate用于非常复杂的布局

时间:2011-10-03 08:16:47

标签: wpf vb.net treeview hierarchicaldatatemplate

我正在制作一个草药数据库程序,我在为主数据类设置HierarchicalDataTemplates时遇到了麻烦。

想要树形布局:

  • 词汇表
    • 组别
      • 信(减少整体清单大小)
        • 条目
          • 注1
          • 注2
  • 草药

    • 植物学名称

      • 按字母
        • 信(减少整体清单大小)
          • 条目
      • 按家庭
        • 家庭
          • 条目
    • 通用名称

      • 信(减少整体清单大小)
        • 条目

注意:所有条目都有注释 注2:Letter是用于排序的字母表中的字母

例如:

  • 一个
    • 苹果
  • b
    • 总线

主类:

Public Class MainSystem
    Public herbs As New Dictionary(Of Integer, herbEntry)
    Public glossary As New Dictionary(Of Integer, glossaryEntry)
End Class

GlossaryEntry:

Public Class glossaryEntry
    Public Property ID As Integer
    Public Property Words As List(Of String) 'Each word is in the format: Catagory/word
    Public Property Notes As SortedDictionary(Of String, String)
End Class

HerbEntry:

Public Class herbEntry
    Public ID As Integer
    Public Property commonName As List(Of String)
    Public Property botanicalName As List(Of String)
    Public Property PlantID As PlantID
End Class
编辑:感谢来自@AngelWPF的链接,我得到了显示我的对象的树。但是,目前这棵树看起来像这样:

  • 草药
    • 通用名称
      • 条目中的CommonName项
      • 条目
      • 中的另一个CommonName项
    • 通用名称
      • 条目中的CommonName项
      • 条目
      • 中的另一个CommonName项

如何进行此更改以匹配我的层次结构?

XAML:

<UserControl.Resources>
        <HierarchicalDataTemplate  DataType="{x:Type my:herbEntry}">
            <TreeViewItem Header="Common Name"  ItemsSource="{Binding commonName}">

            </TreeViewItem>
        </HierarchicalDataTemplate>
    </UserControl.Resources>
    <Grid>
        <TreeView HorizontalAlignment="Stretch" Name="TreeView1" VerticalAlignment="Stretch">
            <TreeViewItem Header="Herbs" ItemsSource="{Binding herbs.Values}"/>
        </TreeView>
    </Grid>

1 个答案:

答案 0 :(得分:0)

此示例可能会指导您为每种类型的绑定项目分配不同级别的不同项目模板...

Having HierarchicalDataTemplates in a TreeView