views:

115

answers:

2

I'm trying to iterate through my Treeview, expanding all nodes however it runs into an InvalidCastException when ran;

Unable to cast object of type 'System.Data.DataRowView' to type 'System.Windows.Controls.TreeViewItem'.

My Code;

foreach (TreeViewItem treeitem in thetreeView.Items)
{
   treeitem.IsExpanded = true;
}

Any ideas? I want to fire this from a button.

+2  A: 

just add this style

<Style TargetType="TreeViewItem"> 
    <Setter Property="IsExpanded" Value="True" /> 
</Style> 

for code please go through this link may be this can help u

http://bea.stollnitz.com/blog/?p=55

Kishore Kumar
Though I'd like todo this at runtime, the initial state of the Treeview should be closed. My idea is to have a collapse/expanded all button.
wonea
for that please read this article with examplehttp://bea.stollnitz.com/blog/?p=55
Kishore Kumar
Agreed. Bea's postings covered the fundamentals of this.
Rob Perkins
Yeah, I looked at that. Confused me somewhat, tried to write something from scratch instead.
wonea
A: 

Bag of tricks has a demo called "TreeView Expand" that has a tree view with expand all and collapse all buttons (and some more)

Nir