【问题标题】:HierarchicalDataTemplate for very complex layoutHierarchicalDataTemplate 用于非常复杂的布局
【发布时间】:2011-10-03 08:16:46
【问题描述】:

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

想要的树形布局:

  • 词汇表
    • 类别1
      • 字母(减少整体列表大小)
        • 条目
          • 注1
          • 注2
  • 草药

    • 植物名称

      • 按字母顺序
        • 字母(减少整体列表大小)
          • 条目
      • 按家庭
        • 家庭
          • 条目
    • 通用名

      • 字母(减少整体列表大小)
        • 条目

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

例如:

  • 一个
    • 苹果
  • b
    • 公交车

主类:

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

词汇表条目:

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>

【问题讨论】:

  • 我仍然没有一个完整的解决方案,@AngelWPF 发布的链接对我开始很有帮助,但我仍然没有找到组织树的方法。到目前为止,我能想到的唯一方法是在我需要的层次结构中生成自定义节点对象并显示它。但这对我来说听起来有点太老套了。在某些方面,WinForms 更容易......

标签: wpf vb.net treeview hierarchicaldatatemplate


【解决方案1】:

这个例子可以指导你在不同级别为每种绑定物品分配不同的物品模板...

Having HierarchicalDataTemplates in a TreeView

【讨论】:

  • 谢谢!那非常有帮助。现在我在获取类别和排序时遇到了问题。用我的部分解决方案更新帖子
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-06
  • 1970-01-01
  • 1970-01-01
  • 2017-01-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多