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


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.

You are binding elements that are already UI Elements (here: TreeViewItems). Normally you bind any objects to your itemsSource and the ItemsContainerGenerator then creates TreeViewItems for each bound item.

This can't be done in your case, you already have TreeViewItems, therefore the Templates can not be applied, and this is why you get this error message.

You could solve it this way:

a) Bind DataObjects to your Tree, not the TreeViewItems, they will be created automatically

b) assign your styles directly to the treeviewItems you already have

See also this link

Need Your Help

kendo Grid grouping incompatibility with row template

kendo-ui kendo-grid

When I'm using other "groupable" row template doesn't work in the kendo grid