2
public void mytree()
{
    DatabaseCore db = new DatabaseCore();

    List<allTreeView> myList = new List<allTreeView>();

    DataTable dt = db.FillDataGrid();
    foreach (DataRow row in dt.Rows)
    {
        string categories_id1 = row["ID"].ToString();
        string parent_id = row["parentId"].ToString();
        string categories_name = row["name"].ToString();
        myList.Add(new allTreeView(
                    int.Parse(categories_id1), 
                    categories_name, 
                    int.Parse(parent_id)));
    }
    Dictionary<int, TreeViewItem> flattenedTree = 
        new Dictionary<int, TreeViewItem>();

    foreach (allTreeView item in myList)
    {

        TreeViewItem treeNode = new TreeViewItem();
        treeNode.Header = item.Header;              //this is header
        treeNode.Tag = item;
        flattenedTree.Add(item.ID, treeNode);
        if (flattenedTree.ContainsKey(item.ParentID))
        {
            flattenedTree[item.ParentID].Items.Add(treeNode);
        }
        else
        {
            product_tree.Items.Add(treeNode);
        }
    }
}

This populates my tree. I want to show context menu on right click for copy, cut, select and add some more functionality on each node of my tree.

4
  • 4
    pls learn MVVM before writing anything in wpf.. you will be surprised how easy it makes life Commented Jun 3, 2014 at 6:01
  • thanx nit but i dont have enough time to study and understand whole tutorial.. i'm stuck on this point of my app. i need a solution.
    – Striker
    Commented Jun 3, 2014 at 6:16
  • @kabir, You should really listen to nit. You don't write code like this in WPF.
    – nakiya
    Commented Jun 3, 2014 at 6:17
  • ya sure nakiya..i will surely study MVVM.. but i was in hurry now my job is done.. i will study MVVM when i get some free time
    – Striker
    Commented Jun 3, 2014 at 7:01

1 Answer 1

3

Create a Treeview in xaml and use binding to populate tree. It's far easier.

then you can do like this:

<TreeView Name="MyTreeview" .........>

 <TreeView.ContextMenu x:Uid="cxt">                            
      <ContextMenu Name="ContextMenu">                                          
       <MenuItem Name="AddNew" Header="Add" Click="AddNew_Click"></MenuItem>
      <Separator/>
      <MenuItem Name="CopyItem" Header="Copy(Ctrl + C)"  Click="CopyItemy_Click"> </MenuItem>

       </ContextMenu>
 </TreeView.ContextMenu>

 ......

</TreeView>
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.