locked
Traversing the Children of a TreeViewItem RRS feed

  • Question

  • I am trying to find all the TreeViewItems of a TreeView item I have selected in my TreeView.

    I can do the opposite easily by using the VisualTreeHelper.GetParent.

    What I can't do is get all the children (just the TreeViewItems) of a selected TreeViewItem. I have tried GetChild & GetChildrenCount, but can't get to the treeviewitem itself.

    Thursday, January 27, 2011 5:19 PM

Answers

  • Hi CraigMuckleston-EMac,

    You can use TreeViewItem's ItemContainerGenerator to get the containers (i.e. treeviewitems) of a TreeViewItem's items.

    Here is a simple example. The following method will retrieve all immediate child TreeViewItem objects of a TreeViewItem.

        List<TreeViewItem> GetChildren(TreeViewItem parent)
        {
          List<TreeViewItem> children = new List<TreeViewItem>();
    
          if (parent != null)
          {
            foreach (var item in parent.Items)
            {
              TreeViewItem child = item as TreeViewItem;
              
              if (child == null)
              {
                child = parent.ItemContainerGenerator.ContainerFromItem(child) as TreeViewItem;
              }
    
              children.Add(child);
            }
          }
    
          return children;
        }
    

    Hope this helps.

    If you still have any questions or concerns about this issue, please feel free to let me know.


    Best regards,
    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, January 28, 2011 7:22 AM

All replies

  • You can loop through the Items of the TreeViewItem.

     

    TVI.Items

    Thursday, January 27, 2011 5:30 PM
  • Hi CraigMuckleston-EMac,

    You can use TreeViewItem's ItemContainerGenerator to get the containers (i.e. treeviewitems) of a TreeViewItem's items.

    Here is a simple example. The following method will retrieve all immediate child TreeViewItem objects of a TreeViewItem.

        List<TreeViewItem> GetChildren(TreeViewItem parent)
        {
          List<TreeViewItem> children = new List<TreeViewItem>();
    
          if (parent != null)
          {
            foreach (var item in parent.Items)
            {
              TreeViewItem child = item as TreeViewItem;
              
              if (child == null)
              {
                child = parent.ItemContainerGenerator.ContainerFromItem(child) as TreeViewItem;
              }
    
              children.Add(child);
            }
          }
    
          return children;
        }
    

    Hope this helps.

    If you still have any questions or concerns about this issue, please feel free to let me know.


    Best regards,
    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, January 28, 2011 7:22 AM