Create your model as follows
public class WrappedNode
{
public string Name { get; set; }
public ObservableCollection<WrappedNode> Nodes { get; set; }
public WrappedNode()
{
Nodes = new ObservableCollection<WrappedNode>();
}
}
Node list you want to associate with treeview
private ObservableCollection<WrappedNode> _nodeList;
public ObservableCollection<WrappedNode> NodeList
{
get { return _nodeList; }
set
{
_nodeList = value;
RaisePropertyChanged(() => NodeList);
}
}
And in xaml:
<TreeView Grid.Row="1"
ItemsSource="{Binding NodeList}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type scnvm:WrappedNode}" ItemsSource="{Binding Nodes}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
If you want a node to have children, just add a node child to the node's Nodes property
source
share