Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1047481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:18:30+00:00 2026-05-16T16:18:30+00:00

I want to dynamically construct a TreeView using Node which represents a typical tree

  • 0

I want to dynamically construct a TreeView using Node which represents a typical tree node. The Node looks like

class Node
{
    public Node(string cont) {
        Content = cont;
        Children = new List<Node>();
    }

    public string Content { get; set; }
    public List<Node> Children { get; set; }
    public bool IsLeaf {
        get { return Children.Count == 0; }
    }
    public bool IsVisible {
        get { return true; }
    }
}

In order to that that I wrote the simple tree traversal which adds TreeViewItems

   void XmlTreeTraversal(DataPoolNode curNode, TreeViewItem curViewNode) {
        if (curNode.IsLeaf)
            return;

        var contentNode = (DataPoolNode)curNode;
        foreach (var node in contentNode.Children) {
            TreeViewItem childViewNode = AddNewNodeToTreeView(node.Content, curViewNode);

            XmlTreeTraversal(node, childViewNode);
        }
    }

    TreeViewItem AddNewNodeToTreeView(string description, TreeViewItem curViewNode) {
        TreeViewItem newTVI = new TreeViewItem();
        newTVI.Header = description;
        curViewNode.Items.Add(newTVI);
        return newTVI;
    }

The problem with the approach is that data and view are intertwined. So It doesn’t meet MVVC. Perhaps, you know another solution for this issue?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:18:31+00:00Added an answer on May 16, 2026 at 4:18 pm

    Do not create the tree yourself. The TreeView-control is one of the conrols that strongly profits from DataBinding and even more from MVVM. Without, it can be a pain to work with. With DataBinding and MVVM, using TreeView is really easy:

    You already have a good source for the TreeView. Its your Node-class. Set a list to the root-node as the ItemsSource of your TreeView…

    m_treeView.ItemsSource=new List<Node>(){yourRootNode};
    

    …Make a HierarchicalDatraTemplate for your nodes. Something like…

    <HierarchicalDataTemplate x:Key="TreeViewItem_HierarchicalDataTemplate" ItemsSource="{Binding Children}" >
             <TextBlock Text="{Binding Content}"  />                
    </HierarchicalDataTemplate>
    

    …and set the ItemsTemplate of the TreeView accordingly…

    <TreeView Name="m_treeView" ItemTemplate="{StaticResource TreeViewItem_HierarchicalDataTemplate}" .../>
    

    If the Node-class is already the ViewModel, it can be interesting to also include an IsSelected and IsExpandend-property and the bind the ItemsContainerStyle-properies to this (do not forget about the INotifyPropertyChanged-event)..

    <TreeView.ItemContainerStyle>                
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
        </Style>
    </TreeView.ItemContainerStyle>
    

    In the beginning it’s a little more work but it can save you hours of time if you then want to do more complicated operations such as automatically selecting and expanding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to dynamically add links to my pages something like this: foreach (Node
I'm using the .order_by() method/function; however, I want to construct the order_by fields dynamically.
In a popup window, I create a string dynamically which represents a function existing
I want to dynamically create instance method of child class through class method of
I want to dynamically create controls in my bean. I am using JSF 2.0
I want to dynamically construct a find command and see whether it returns anything.
I want to dynamically load a dll and construct a MyDllClass object. My code
say i have this class Framework.Asd.Human with a public empty constructor. and i want
I used a function to construct an image path dynamically like this: function ()
Assuming I can construct a string that matches an existing class, how do I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.