Home » WPFRSS

How to ADD child Nodes to Treeview in WPF, using C# code.

+CEO
     +MANGER1
          PERSON1
     +MANGER2
          PERSON2

I CAN DO THIS USING XAML CODE. But I Dont Know How to do this Using C# code...... can anybody Help ME plzzzz.......

Thank you ,
VENU.
 

4 Answers Found

 

Answer 1

He you are:

            TreeViewItem ceo = new TreeViewItem() { Header = "CEO" };
            TreeViewItem manager1 = new TreeViewItem() { Header = "Manager1" };
            TreeViewItem manager2 = new TreeViewItem() { Header = "Manager2" };
            TreeViewItem person1 = new TreeViewItem() { Header = "person1" };
            TreeViewItem person2 = new TreeViewItem() { Header = "person2" };

            manager1.Items.Add( person1 );
            manager2.Items.Add( person2 );
            ceo.Items.Add( manager1 );
            ceo.Items.Add( manager2 );
            tree.Items.Add( ceo );
Cheers.


 

Answer 2

Thanks vtortola. See my problem is not that example. It is like this...
so here it is.... in WPF... Treeview control--

i can add  nodes under root node, using XAML code  which is static

i hav to add the nodes, using C# code  which is dynamic. For Example: Menu Table

MenuIdNameParentId
    1           Country         0
    2           India             1
    3           USA              1
    4           AP                2
    5           UP                2
    6           California      3
    7           Newyork       3


In my C# Code i took a class file in that, i declared the menu() method of type DataTable.

Method menu():- I created a temptable(using DATATABLE CLASS) with same fields(using CLONE()) and filled it with MENU table data and returned that temptable.

So now i have data with me.
Based on ParentID i can retrieve thechildvalues.

but how to add those values to treeview  as its ITEMS
There is no---> " treeview1.Items[0].Items.Add() " thing in WPF--- n its creating problem to me.

Hw can i do dat...? i dnt knw which Methods, Properties, Classes  to Use.
Ex:
      + COUNTRY
            + INDIA
                   AP
                   UP
             +USA
                   Lusiana
                   Nwyrk


Please help.
Thank you.

Beginner in WPF..... VENU.
 

Answer 3


Just give it a try..
I solved my problem with this.... THANKS TO.. " NOORBAKSH ".. for helping me with this...

Here is a simplified example of code  that runs exactly the way you want FOR the ABOVE Kind of Data..:

In xaml:

<Grid>
<TreeView Name="myTreeView" />
</Grid>


In code:

public partial class Window11 : Window
{
public Window11()
{
InitializeComponent();

//I created my objects here, since I have no DataTable
//It is assumed the the Parents appear before any child
List<MyMenuItem> myList = new List<MyMenuItem>();
myList.Add(new MyMenuItem(1, "Country", 0));
myList.Add(new MyMenuItem(2, "U.S.A.", 1));
myList.Add(new MyMenuItem(3, "India", 1));
myList.Add(new MyMenuItem(4, "AP", 3));
myList.Add(new MyMenuItem(5, "UP", 3));
myList.Add(new MyMenuItem(6, "CA", 2));
myList.Add(new MyMenuItem(7, "NY", 2));


Dictionary<int, TreeViewItem> flattenedTree = new Dictionary<int, TreeViewItem>();


foreach (MyMenuItem item in myList)
{
TreeViewItem treeNode = new TreeViewItem();
treeNode.Header = item.Header;
treeNode.Tag = item;
flattenedTree.Add(item.ID, treeNode);


if (flattenedTree.ContainsKey(item.ParentID))
{
flattenedTree[item.ParentID].Items.Add(treeNode);
}
else
{
myTreeView.Items.Add(treeNode);
}
}
}
}

class MyMenuItem
{
internal int ID
{private set;get;}

internal string Header
{ private set; get; }

internal int ParentID
{ private set; get; }

internal MyMenuItem(int id, string header, int parentId)
{
ID = id;
Header = header;
ParentID = parentId;
}
}

Hope this gets you going.


noorbakhsh
 

Answer 4

Ya... Its correct.But it is hardcoded...! Can you add  treeview items without hardcode that to i should retreview values from database.The database values should be added as items in treeview.Can anyone slve this problem?
SHANKARNAG.T
 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter