Can't get TreeViewItems to expand!!
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
- 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
- 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 oh wow.....
well.....it worked :-)
It's a lot of code. But now that I look at it, it really does make sense.
This is the best way I found:
http://shevaspace.spaces.live.com/blog/cns!FD9A0F1F8DD06954!581.entry
Code Snippetusing 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¡¡¡¡


