.NET Framework Developer Center > .NET Development Forums > Windows Presentation Foundation (WPF) > having troubles inserting a new node in a treeview
Ask a questionAsk a question
 

Questionhaving troubles inserting a new node in a treeview

  • Thursday, October 22, 2009 4:20 PMCarlos de Luna Saenz Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have a program that has a feature to add a node in a treeview, if the treeview item that will hold the new node has no childs a new List<TreeViewItem> with the new node is asigned to the ItemsSource property, the new node is displayed but the expand simbol (+) in the treeviewitem where the new node was added is not shown.
    ¿Any ideas?
    Greetings.

All Replies

  • Thursday, October 22, 2009 11:27 PMBigsby Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    List<TreeViewItem> should be of type ObservableCollection for VisualTree to listen to its change. But...

    You should not add TreeViewItems to TreeView or TreeViewControls. Conceptually, these are meant to display hierarchichal data so you should add child node object to parent node object.


    Bigsby, Lisboa, Portugal
    O que for, quando for, é que será o que é...
    Wenn ist das Nunstruck git und Slotermeyer? Ja! ... Beiherhund das Oder die Flipperwaldt gersput!
    http://bigsby.eu
  • Wednesday, October 28, 2009 6:22 AMBruce.ZhouMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Carlos,

    I can not understand why "+" symbol is not expected to be shown when it has multiple child elments, would you please clarify?


    Best regards,
    Bruce Zhou
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Thursday, November 05, 2009 2:55 AMCarlos de Luna Saenz Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Imagine i have something like:
    if(myTreeViewItem.ItemsSource ==null)
    {
       List<TreeViewItem> myChilds = new List<TreeViewItem>();
       myChilds.Add(theNewNode;
       myTreeViewItem.ItemsSource =myChilds;
    }
    
    my screen shows the "myTreeViewItem" and "myChilds" contents but theres no "expantion + sign" in the left side of the "myTreeViewitem" tvi...
  • Thursday, November 05, 2009 4:12 AMRahul P Nath Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,
    Can you post the full code.

    Thank You
    Please mark posts as answers/helpful if it answers your query. This would be helpful for others facing the same kind of problem
  • Friday, November 06, 2009 7:12 PMCarlos de Luna Saenz Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
            private void MueveNodo(List<TreeViewItem> itemSelecPadre)
            {
                if (itemSelecPadre.Count > 1)
                {
                    List<TreeViewItem> hijos = (List<TreeViewItem>)itemSelecPadre[1].ItemsSource;
                    hijos.Remove(itemSelecPadre[0]);
                    itemSelecPadre[1].Items.Refresh();
                }
                else
                {
                    List<TreeViewItem> hijos = (List<TreeViewItem>)Ventana.treeView.ItemsSource;
                    hijos.Remove(itemSelecPadre[0]);
                    Ventana.treeView.Items.Refresh();
                }
                if (target.ItemsSource != null)
                {
                    List<TreeViewItem> hijos = (List<TreeViewItem>)target.ItemsSource;
                    hijos.Add(itemSelecPadre[0]);
                    target.IsExpanded = true;
                    target.Items.Refresh();
                }
                else
                {
                    List<TreeViewItem> hijos = new List<TreeViewItem>();
                    target.ItemsSource = hijos;
                    hijos.Add(itemSelecPadre[0]);
    
                    target.IsExpanded = true;
                }
                IFachadaTesauro fac = new FachadaTesauro();
                TemaTO padre = (TemaTO)target.Tag;
                TemaTO hijo = (TemaTO)itemSelecPadre[0].Tag;
                hijo.IDPadre = padre.IDTema;
                int error = fac.actualizaPadre(hijo);
                if (hijo.Materia != padre.Materia)
                {
                    error = fac.actualizaMateria(hijo.IDTema, padre.Materia);
                }
                Ventana.treeView.UpdateLayout();
            }
    
    The specific part where the trouble is is on the
    if (target.ItemsSource != null)
    block.
  • Saturday, November 07, 2009 1:56 PMRammohan Ammiti Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     


    Hi


    Here, i am sending the example of code test it ur side. it's working my side perfectly


    http://www.sendspace.com/file/g0pzws