简体   繁体   中英

WPF TreeView - adding TreeNode with children

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.

.eg :

-Parent

--Child

----Child

--Child

...

But In the wpf window I'm getting only the parent node. without the expand/collapse buttons.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM