简体   繁体   English

WPF树视图ItemTemplate和ItemTemplateSelector被忽略

[英]WPF treeview ItemTemplate and ItemTemplateSelector are ignored

In my application, I have been getting this error each time the treeView loads it's items. 在我的应用程序中,每次treeView加载它的项目时我都会收到此错误。 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' 错误是: System.Windows.Data Error: 26 : ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type; Type='TreeViewItem' 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) 我对树视图的xaml代码是:(我在我的ViewModel上使用VB.net)

<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. ItemsSource的绑定是一个ObservableCollection(Of TreeViewItem),它是从数据库中填充的。

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 不要在视图模型中创建TreeViewItems列表,而是创建一个对象列表,它只是描述您想要显示的数据,即使它很简单。

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

TreeViewItem already has a template associated with it. TreeViewItem已经有一个与之关联的模板。

You are binding elements that are already UI Elements (here: TreeViewItems). 您是已经是UI元素的绑定元素(此处为:TreeViewItems)。 Normally you bind any objects to your itemsSource and the ItemsContainerGenerator then creates TreeViewItems for each bound item. 通常,您将任何对象绑定到itemsSource,然后ItemsContainerGenerator为每个绑定项创建TreeViewItem。

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. 在您的情况下无法完成此操作,您已经拥有TreeViewItems,因此无法应用模板,这就是您收到此错误消息的原因。

You could solve it this way: 你可以这样解决:

a) Bind DataObjects to your Tree, not the TreeViewItems, they will be created automatically a)将DataObjects绑定到Tree,而不是TreeViewItems,它们将自动创建

b) assign your styles directly to the treeviewItems you already have b)将你的样式直接分配给你已经拥有的treeviewItems

See also this link 另见此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM