wpfvb.nettreeviewitemtemplateitemtemplateselector

WPF treeview ItemTemplate and ItemTemplateSelector are ignored


In my application, I have been getting this error each time the treeView loads it's items. This error makes my application slow on load and takes at least two minutes to load.

The error is: System.Windows.Data Error: 26 : ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type; Type='TreeViewItem'

My xaml code for the treeview is: (I'm using VB.net on my ViewModel)

<TreeView ScrollViewer.VerticalScrollBarVisibility="Hidden" 
          ItemContainerStyle="{DynamicResource tviStyle}" 
          Background="#FFF0F0F0" BorderBrush="#FFE5E2DB" 
          IsEnabled="{Binding isTreeEnable}" 
          ItemsSource="{Binding PostcodeLijst}" 
          Margin="0" Name="dgStamOverzichtPostcode" />

The binding of the ItemsSource is an ObservableCollection(Of TreeViewItem) thats is filled from a database.

I have looked in google for answer but so far I have been not been able to find it! Does anybody knows how to fix this?

Thanks in advance for any help


Solution

  • Instead of creating a list of TreeViewItems in your view model, create a list of objects that simply describe the data that you want to show, even if it's as simple as

    class Item
    {
       public string Header { get;set; }
    }
    

    TreeViewItem already has a template associated with it.