So, I modified your code according to my comment above. BinaryTreeData now has a list of SubItems. You will need to configure the namespace in XAML / local: BinaryTreeData, and it should work. Greetings!
BinaryTreeData:
public class BinaryTreeData : INotifyPropertyChanged { private int _ownID; private int _parentID; public int ownID { get { return this._ownID; } set { this._ownID = value; this.onChange("ownID"); } } private List<BinaryTreeData> _subitems = new List<BinaryTreeData>(); public List<BinaryTreeData> Subitems { get { return _subitems; } } public event PropertyChangedEventHandler PropertyChanged; private void onChange(string propertyName) { if (PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }
XAML:
<TreeView x:Name="myTreeView"> <TreeView.ItemTemplate> <HierarchicalDataTemplate DataType="{x:Type local:BinaryTreeData}" ItemsSource="{Binding Subitems}"> <TextBlock Text="{Binding Path=ownID}" /> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView>
CodeBehind:
public MainWindow() { InitializeComponent(); List<BinaryTreeData> myBinaryData = new List<BinaryTreeData>(); BinaryTreeData parent1 = new BinaryTreeData() { ownID = 1 }; parent1.Subitems.Add(new BinaryTreeData { ownID = 2 }); parent1.Subitems.Add(new BinaryTreeData { ownID = 3 }); BinaryTreeData parent2 = new BinaryTreeData() { ownID = 4 }; parent2.Subitems.Add(new BinaryTreeData { ownID = 5 }); parent2.Subitems.Add(new BinaryTreeData { ownID = 6 }); myBinaryData.Add(parent1); myBinaryData.Add(parent2); myTreeView.ItemsSource = myBinaryData; }
Sveng source share