Reputation: 23
I have TreeNode object [namespace System.Windows.Forms] and I have WPF TreeView Control.
I'm trying to populate this wpf control with the TreeNode data by this code:
public partial class TreeWindow : Window
{
public TreeWindow(TreeNode node)
{
InitializeComponent();
treeView.Items.Add(node);
}
}
This TreeNode contains many children in a tree hierarchy.
.e.g :
-Parent
--Child
----Child
--Child
...
But In the wpf window I'm getting only the parent node. without the expand/collapse buttons.
Upvotes: 1
Views: 4611
Reputation: 17085
You have to convert them to System.Windows.Controls.TreeViewItem
first.
public TreeWindow(TreeNode node)
{
InitializeComponent();
treeView.Items.Add(ConvertToWpf(node));
}
TreeViewItem ConvertToWpf(TreeNode node)
{
var wpfItem = new TreeViewItem();
wpfItem.Header = node.Text;
foreach(var child in node.Nodes)
{
wpfItem.Items.Add(ConvertToWpf(child));
}
return wpfItem;
}
Upvotes: 3