Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 960
Rui Avatar asked Feb 21 '23 04:02

Rui


2 Answers

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.

like image 156
Andy Avatar answered Mar 09 '23 01:03

Andy


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

like image 44
SvenG Avatar answered Mar 09 '23 00:03

SvenG