Question

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.

Était-ce utile?

La solution

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;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top