There is such

<TreeView Height="414" HorizontalAlignment="Left" Margin="-6,-2,0,0" Name="treeView1" ItemsSource="" VerticalAlignment="Top" Width="142" Padding="0"> </TreeView> 

The task is to add the Drinks node and to it the child nodes 1 2 3 4 That is, to get something like this:

 <TreeView Height="414" HorizontalAlignment="Left" Margin="-6,-2,0,0" Name="treeView1" ItemsSource="" VerticalAlignment="Top" Width="142" Padding="0"> <TreeViewItem Header="Drinks"> <TreeViewItem Header="1"></TreeViewItem> <TreeViewItem Header="2"></TreeViewItem> <TreeViewItem Header="3"></TreeViewItem> <TreeViewItem Header="4"></TreeViewItem> </TreeViewItem> </TreeView> 

How to implement it programmatically?

    1 answer 1

    TreeViewItem is a hereditary ItemsControl , it has an Items property in which you can add items. All the rest is an external loop for adding parent elements, for each parent a separate sub-cycle for the child nodes. Although this is not best practice, but the answer to your question.

    Feng Shui is to implement a binding to some data (to the CollectionViewSource or collection property from the ViewModel, using this link is an example of a TreeView ) and use the HierarchicalDataTemplate to determine the type of elements in the tree.

    • Those. As I understand it, you can bind to CollectionViewSource, and continue to work with CollectionViewSource? - or_die
    • Yes that's right. - Eugene Cheverda
    • And can you see an example somewhere? - or_die
    • You can follow the links in the answer. - Eugene Cheverda