HierarchicalDataTemplate and DataType: XAML parser error
-
Thursday, March 01, 2012 1:28 PM
Hi,
I'm trying to use in my MVVM SL5 application a TreeView with a HierarchicalDataTemplate with a couple of implicit data templates, one for each of my viewmodels. The code compiles fine, but at runtime I get a XamlParserException telling that "Items added to a dictionary must have a key". Of course I can add the key, but this defeats the whole purpose of implicit type-based templating, and how could I work around this in the context of a hierarchical template to be used as source of a TreeView? Could anyone help? Here is a dummy code fragment similar to my real-world XAML:
<sdk:TreeView ItemsSource="{Binding Path=Segments,Mode=OneWay}">
<sdk:TreeView.ItemContainerStyle>
<Style TargetType="sdk:TreeViewItem">
<Setter Property="IsExpanded" Value="{Binding Path=IsExpanded,Mode=TwoWay}"/>
<Setter Property="IsSelected" Value="{Binding Path=IsSelected,Mode=TwoWay}"/>
<Setter Property="FontWeight" Value="Normal"/>
</Style>
</sdk:TreeView.ItemContainerStyle><sdk:TreeView.Resources>
<sdk:HierarchicalDataTemplate DataType="dummy:Customer"
ItemsSource="{Binding Path=Products,Mode=OneWay}">
...template...
</sdk:HierarchicalDataTemplate><DataTemplate DataType="dummy:Product">
...template...
</DataTemplate>
</sdk:TreeView.Resources>
</sdk:TreeView>
All Replies
-
Friday, March 02, 2012 6:27 AM
Hi
Refer the Thred and a sample is there about implicit DataTemplates
-
Friday, March 02, 2012 12:57 PM
Thank you, I checked the sample but it does not seem to be different from my code, apart from the fact that the templates are declared in a separate resources dictionary. I tried moving them upper into the resources of the usercontrol containing the treeview, but nothing changes. Yet, the syntax should be simple: DataType="mynamespace:MyType". Any suggestion?
-
Friday, March 02, 2012 4:08 PM
Here is a complete repro solution, showing exactly the error I'm talking about. Could anyone give a suggestion?
-
Sunday, March 04, 2012 11:20 PM
you are using
HierarchicalDataTemplate
Change
HierarchicalDataTemplate to DataTemplate.. your solution works..
HierarchicalDataTemplate is not supported for implicit conversion..
-
Monday, March 05, 2012 8:51 AM
Thank you, I did not know about this limitation!

