Die Items-Sammlung muss vor dem Verwenden von ItemsSource leer sein.

Maik Smith 21 Reputation points
2020-05-28T14:58:38.607+00:00

Hallo!

Ich möchte ein hierarchical TreeView aus einem hierarischen Datamodel erstellen.

Die ItemsSource des TreeView binde ich an das Datamodel über die DataSource des Treeviews.

<TreeView ItemsSource="{Binding ListHDItems}"/>  

8669-treeview-ohne-datatemplate.jpg

TreeView ohne DataTemplate

Wie erwartet, werden die beiden (im Datamodel definierten) Einträge angezeigt.

Wenn ich jetzt jedoch auch nur ein DataTemplate zur formatierten Anzeige der Einträge einfüge, kommt die Fehlermeldung:

Die Items-Sammlung muss vor dem Verwenden von "ItemsSource" leer sein.
<TreeView ItemsSource="{Binding ListHDItems}">
<HierarchicalDataTemplate DataType="{x:Type local:HD_Item}" >
<TextBlock Text="{Binding Item_Bezeichnung}" />
</HierarchicalDataTemplate>
</TreeView>

8700-fehlermeldung.jpg

Woran kann das denn liegen?

Fred.

======================================

Das Datamodel:
public class HierarchicalData : INotifyPropertyChanged
{
#region PropertyChanged ------------------------
public event PropertyChangedEventHandler PropertyChanged; // Event-Handler für Change-Ereignis definieren
private void Notify(string argument) // PropertyChanged auslösen
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(argument));
}
#endregion

public HierarchicalData(List&lt;HD_Item&gt; _ListHDItems = null)  
{  
    if (_ListHDItems != null) ListHDItems = _ListHDItems;  
    else ListHDItems = new List&lt;HD_Item&gt;();  
}  

private List&lt;HD_Item&gt; _ListHDItems;  
public List&lt;HD_Item&gt; ListHDItems  
{  
    get { return _ListHDItems; }  
    set { _ListHDItems = value; Notify(&#34;ListHDItems&#34;); }  
}  

}

/// <summary>
/// Eintrag, der in einer hierarischen Auflistung benutzt werden kann.
/// </summary>
public class HD_Item : INotifyPropertyChanged
{
#region PropertyChanged ------------------------
public event PropertyChangedEventHandler PropertyChanged; // Event-Handler für Change-Ereignis definieren
private void Notify(string argument) // PropertyChanged auslösen
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(argument));
}
#endregion

/// &lt;summary&gt;  
/// Hierarischer Eintrag  
/// &lt;/summary&gt;  
/// &lt;param name=&#34;_Item_Bezeichnung&#34;&gt;Bezeichnung des Eintrages.&lt;/param&gt;  
/// &lt;param name=&#34;_ListHDItems&#34;&gt;Liste der untergeordneten Einträge.&lt;/param&gt;  
/// &lt;param name=&#34;_Fett&#34;&gt;Soll der hierarische Eintrag fett dargestellt werden.&lt;/param&gt;  
/// &lt;param name=&#34;_IsExpanded&#34;&gt;Sollen untergeordnete Einträge angezeigt werden? (aufgeklappt?)&lt;/param&gt;  
public HD_Item(string _Item_Bezeichnung, List&lt;HD_Item&gt; _ListHDItems=null, FontWeight? _Fett = null, bool? _IsExpanded = null)  
{  
    Item_Bezeichnung = _Item_Bezeichnung;   
    if (_ListHDItems != null) ListHDItems = _ListHDItems;  
    else ListHDItems = new List&lt;HD_Item&gt;();  
    if (_Fett != null) Fett = (FontWeight)_Fett;  
    else Fett = FontWeights.Normal;  
    if (_IsExpanded != null) IsExpanded = (bool)_IsExpanded;  
    else IsExpanded = false;  
}  

private string _Strg_Item_Bezeichnung = string.Empty;  
public string Item_Bezeichnung   
{   
    get { return _Strg_Item_Bezeichnung; }   
    set { _Strg_Item_Bezeichnung = value; Notify(&#34;Item_Bezeichnung&#34;); }   
}  
      
private List&lt;HD_Item&gt; _ListHDItems = new List&lt;HD_Item&gt;();  
public List&lt;HD_Item&gt; ListHDItems  
{  
    get { return _ListHDItems; }  
    set { _ListHDItems = value; Notify(&#34;ListHDItems&#34;); }  
}  

private FontWeight _Fett = FontWeights.Normal;  
public FontWeight Fett   
{   
    get { return _Fett; }  
    set { _Fett = value;  Notify(&#34;Fett&#34;); }  
}  

private bool _IsExpanded = false;  
public bool IsExpanded  
{  
    get { return _IsExpanded; }  
    set { _IsExpanded = value; Notify(&#34;IsExpanded&#34;); }  
}  
 

}

Die erstellten Daten:
public MainWindow()
{
InitializeComponent();
HierarchicalData dm = new HierarchicalData();
HD_Item Item1_1 = new HD_Item("Kündigungsgründe", new List<HD_Item> {new HD_Item("Standard")}, FontWeights.Bold, true);
HD_Item Item1_2 = new HD_Item("Leerstands-Auswertung", new List<HD_Item> { new HD_Item("Leerstandsliste")}, FontWeights.Bold, true);
HD_Item Item0_2 = new HD_Item("Controlling", new List<HD_Item> { new HD_Item("Gesamtliste")}, FontWeights.Bold, true);
HD_Item Item0_1 = new HD_Item("Allgemein", new List<HD_Item> { Item1_1, Item1_2 });
dm.ListHDItems.Add(Item0_1); dm.ListHDItems.Add(Item0_2);
TV_Reportauswahl.DataContext = dm;
}

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,663 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2020-05-28T20:38:36.89+00:00

    Hi Fred, Ein DataTemplate hat in einem TreeView nichts zu suchen. Da sollte eine Ressource mit HierarchicalDataTemplate mit Bezug auf den Typ das darzustellenden Datenobjektes genutzt werden. Zeig mal den von dir genutzten XAML des TreeView und der genutzten Ressourcen.

    0 comments No comments