HierarchicalDataTemplate and DataType: XAML parser error

Answered 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