【发布时间】:2015-05-13 15:39:54
【问题描述】:
我正在尝试构建一个具有三层的 WPF TreeView。 CountryReportTitle 是一个字符串属性,ArticleCategoryTitlesList 是一个集合,两者都暴露于我的ViewModel。没有定义类层次结构。这是我正在寻找的结构:
这是我尝试的 Xaml,但我在运行时在 Xaml 中遇到异常:
{"Item has already been added. Key in dictionary: 'DataTemplateKey(ISESApp.ViewModels.ReportViewModel)' Key being added: 'DataTemplateKey(ISESApp.ViewModels.ReportViewModel)'"}
Xaml:
<TreeView ItemsSource="{Binding CountryReportTitle}">
<TreeView ItemsSource="{Binding CountryReportTitle}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:ReportViewModel}"
ItemsSource="{Binding ArticleCategoryTitlesList}">
<TextBlock Text="{Binding CategoryTitle}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:ReportViewModel}"
ItemsSource="{Binding ArticleCatagoryTypesList}">
<TextBlock Text="{Binding ArticleTitle}" />
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type local:ReportViewModel}">
<TextBlock Text="{Binding ArticleTitle}" />
</DataTemplate>
</TreeView.Resources>
</TreeView>
</TreeView>
Local: 是我的 ViewModel 的命名空间:
xmlns:local="clr-namespace:MyApp.ViewModels"
我做错了什么,解决这个问题的最佳方法是什么?
【问题讨论】: