下列代码的
<TreeView Name="tree" ItemsSource="{Binding Path=AllNotes}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type m:Note}" ItemsSource="{Binding Path=ListIssuesType}">
<TextBlock Text="{Binding Path=SoftwareVersion}" Margin="2" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type m:IssueType}" ItemsSource="{Binding Path=IssueNames}">
<TextBlock Text="{Binding Path=IssueTypeName}" Margin="2" />
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type m:IssueType}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=IssueTypeName}" />
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>我知道错误:
“项已经添加。字典中的键:正在添加的'DataTemplateKey(ReleaseNotes_Window.Models.IssueType)‘键:'DataTemplateKey(ReleaseNotes_Window.Models.IssueType)'”
发布于 2016-01-20 20:21:51
当您将某些内容放入ResourceDictionary中时,它要么需要显式的x:Key,要么键将由应用于该类的DictionaryKeyPropertyAttribute来确定。
对于DataTemplate,如下所示:
[DictionaryKeyProperty("DataTemplateKey")]
public class DataTemplate : FrameworkTemplate这将取决于DataType。
因为你有:
<HierarchicalDataTemplate DataType="{x:Type m:IssueType}" ItemsSource="{Binding Path=IssueNames}">
<TextBlock Text="{Binding Path=IssueTypeName}" Margin="2" />
</HierarchicalDataTemplate>和
<DataTemplate DataType="{x:Type m:IssueType}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=IssueTypeName}" />
</StackPanel>
</DataTemplate>无论是使用DataType="{x:Type m:IssueType}",这就是程序失败的原因。
对其中一个x:Key使用额外的DataTemplate,并将其作为StaticResource引用,您打算在其中使用它。
https://stackoverflow.com/questions/34909230
复制相似问题