문제

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.

도움이 되었습니까?

해결책

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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top