4

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

akjoshi
  • 15,374
  • 13
  • 103
  • 121
Rui
  • 387
  • 7
  • 22

2 Answers2

4

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.

Andy
  • 6,366
  • 1
  • 32
  • 37
  • 2
    I agree with Andy, have a look at this article - [Simplifying the WPF TreeView by Using the ViewModel Pattern](http://www.codeproject.com/Articles/26288/Simplifying-the-WPF-TreeView-by-Using-the-ViewMode) – akjoshi Jun 12 '12 at 08:44
4

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

Community
  • 1
  • 1
SvenG
  • 5,155
  • 2
  • 27
  • 36
  • About the A option, your saying to me to change the property PostcodeLijst from ObservableCollection(of TreeViewItem) to something else?! Im not getting the point here :P – Rui Jun 12 '12 at 09:39
  • What I mean is that PostcodeLijst shouldn't be an ObservableCollection but rather a simple data representation e.g. ObservableCollection. in your current implementation your ViewModel knows that the PostCodes are displayed as TreeViewItems. But thats not its job (and by the way isn't MVVM like). What if you like to change your tree to something else like a listview? My suggested option a) is exactly the same as the user "Andy" already suggested in his post and I highly agree with it. – SvenG Jun 12 '12 at 10:21