Ask a questionAsk a question
 

AnswerCan't get TreeViewItems to expand!!

  • Friday, January 19, 2007 3:19 PMhoutexwebdev Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    IF I did this:

    <TreeView>
       <TreeViewItem>
           <TreeViewItem>
          <TreeViewItem>
       </TreeViewItem>
    <TreeView>

    Life would be good.  I can loo at the TreeView's Items collection, which is a collection of TreeViewITems....each of those have TreeViewItems as well.  All I have to do is find the TreeViewItem I want (navigating from the parent TreeviewItem so that the entire path is expanded) and say IsExpanded=true.

    BUT...it doesn't work that way for me.

    Instead of explicitly adding the items in a source xaml document, I am letting the treeview populate recursively using a Hierarchical data template that is bound to a class called "Folder."  This class has a property called "Subfolders" which contains an Observable collection of more folder instances.

    Therefore, my XAML pretty much looks like:

    ...some XAML...

    <HierarchicalDataTemplate DataType="{x:Type c:Folder}" ItemsSource="{Binding Subfolders}" >
         <
    TextBlock Text="{Binding Name}" />
    </
    HierarchicalDataTemplate>

    ...some XAML...

    <TreeView Name="FolderExplorer" SelectedValuePath="UncPath" />

    ...more XAML...

    When I check The Treeview, it DOES have an Items Collection, but it returns a collection of Folder instances, not TreeViewItems.  I can not expand the TreeViewItems because I can't get a reference to the control.

    Ultimately, I'd like to be able to pull up a list of folders and, if there is a known selected folder for a UNC path, have that folder expanded.  I'm not sure how this should work.

    I tried adding a property to the Folder class named "IsInInitialUncPath" so taht the HierarchicalDataTemplate could be expanded when it binds.  However, there is no IsExpanded or IsSelected property available in the HierarchicalDataTemplate that I can bind to.

    Any Ideas?

Answers

  • Friday, January 19, 2007 4:30 PMZhou Yong Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    I have a sample on my blog which demonstrates how to do it:

    The Tricks with TreeViewItem Expansion

    BTW: Someone complains the download link is broken, I've tested it, and it works perfectly in my region, if you have the same problem downloading the sample, please let me know, I will try to fix it.

    Sheva

All Replies

  • Friday, January 19, 2007 4:30 PMZhou Yong Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    I have a sample on my blog which demonstrates how to do it:

    The Tricks with TreeViewItem Expansion

    BTW: Someone complains the download link is broken, I've tested it, and it works perfectly in my region, if you have the same problem downloading the sample, please let me know, I will try to fix it.

    Sheva
  • Friday, January 19, 2007 8:34 PMhoutexwebdev Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    oh wow.....

    well.....it worked :-)

    It's a lot of code.  But now that I look at it, it really does make sense.

     

  • Monday, September 10, 2007 10:25 AMDubhe Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    This is the best way I found:

    http://shevaspace.spaces.live.com/blog/cns!FD9A0F1F8DD06954!581.entry


    Code Snippet

    using System;
    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;

    namespace Sheva.Windows.Controls
    {
        public static class TreeViewHelper
        {
            public static void ExpanAll(TreeView treeView)
            {
                ExpandSubContainers(treeView);
            }

            private static void ExpandSubContainers(ItemsControl parentContainer)
            {
                foreach (Object item in parentContainer.Items)
                {
                    TreeViewItem currentContainer = parentContainer.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
                    if (currentContainer != null && currentContainer.Items.Count > 0)
                    {
                        currentContainer.IsExpanded = true;
                        if (currentContainer.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated)
                        {
                            currentContainer.ItemContainerGenerator.StatusChanged += delegate
                            {
                                ExpandSubContainers(currentContainer);
                            };
                        }
                        else
                        {
                            ExpandSubContainers(currentContainer);
                        }
                    }
                }
            }
        }
    }



    Thanks Sheva¡¡¡¡