SIGN UP MEMBER LOGIN:    
ARTICLE

Expand and Collapse all nodes of a Treeview in Silverlight 3

Posted by Diptimaya Patra Articles | XAML with C# August 06, 2009
This is all about working with nodes of a Treeview in Silverlight 3.
Reader Level:
 


Introduction

In this article we will explore about Tree View control in Silverlight 3. We will see how can we expand and Collapse all in a single go.

Creating Silverlight Project

Fire up Expression Blend 3 and create a Silverlight Application. Name it as ExpandCollapseTreeViewSL3.

ima1.gif

Now go ahead and add a Tree View control to your application.

Then you can add TreeViewItem into your Tree View.

ima2.gif

Add several, and if you want to add children of any parent then select the particular TreeViewItem and add another Treeview Item.

I have created the below hierarchy:

ima3.gif

If you see the design view, it will look similar to the following:

ima4.gif

Here is the XAML code behind for the TreeView:

<controls:TreeView x:Name="MyTreeView" HorizontalAlignment="Left" Width="197" Margin="0,0,0,202">

  <
controls:TreeView.Background>
    <
LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
      <
GradientStop Color="LightBlue" Offset="0"/>
      <
GradientStop Color="White" Offset="1"/>
    </
LinearGradientBrush>
  </
controls:TreeView.Background>
  <
controls:TreeViewItem Margin="0" Header="Item 1">
    <
controls:TreeViewItem Margin="0" Header="Sub Item 1"/>
  </
controls:TreeViewItem>
  <
controls:TreeViewItem Margin="0" Header="Item 2">
    <
controls:TreeViewItem Margin="0" Header="Sub Item 1"/>
    <
controls:TreeViewItem Margin="0" Header="Sub Item 2"/>
  </
controls:TreeViewItem>
  <
controls:TreeViewItem Margin="0" Header="Item 3">
    <
controls:TreeViewItem Margin="0" Header="Sub Item 1">
      <
controls:TreeViewItem Margin="0" Header="Sub Item 1"/>
    </
controls:TreeViewItem>
  </
controls:TreeViewItem>
</
controls:TreeView>

We will see how can we Expand all or Collapse all in a single button click.

Add two Buttons to your application saying Expand All and Collapse all.

ima5.gif

ima6.gif

Now add Click Events for the above two Buttons.

<
Button x:Name="btnExpand" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="75" Content="Expand All" Margin="0,0,0,176" Click="btnExpand_Click"/>
<
Button x:Name="btnCollapse" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="75" Content="Collapse All" Margin="0,0,2,176" Click="btnCollapse_Click"/>

Expanding All

Now in the Event Handler for btnExapnd write the following code:

private
void btnExpand_Click(object sender, System.Windows.RoutedEventArgs e)
{

for
(int i = 0; i < MyTreeView.Items.Count; i++)
{
ExpandAllTreeViewItems((TreeViewItem)MyTreeView.ItemContainerGenerator.ContainerFromIndex(i));
}
}


Add the method ExpandAllTreeViewItems, and add following code:

private
void ExpandAllTreeViewItems(TreeViewItem currentTreeViewItem)
{

if
(!currentTreeViewItem.IsExpanded)
{
currentTreeViewItem.IsExpanded = true;
currentTreeViewItem.Dispatcher.BeginInvoke(() => ExpandAllTreeViewItems(currentTreeViewItem));
}

else

{

for
(int i = 0; i < currentTreeViewItem.Items.Count; i++)
{
TreeViewItem child = (TreeViewItem)currentTreeViewItem.ItemContainerGenerator.ContainerFromIndex(i);
ExpandAllTreeViewItems(child);
}
}
}


Collapsing All

Now in the Event Handler of btnExpandAll add the following code:

private
void btnCollapse_Click(object sender, RoutedEventArgs e)
{

for
(int i = 0; i < MyTreeView.Items.Count; i++)
{
CollapseAllTreeViewItems((TreeViewItem)MyTreeView.ItemContainerGenerator.ContainerFromIndex(i));
}
}


Add the method CollapseAllTreeViewItems, and add the following code:

private
void CollapseAllTreeViewItems(TreeViewItem rootTreeViewItem)
{
Stack<TreeViewItem> treeViewItemsStack = new Stack<TreeViewItem>();
treeViewItemsStack.Push(rootTreeViewItem);

while
(treeViewItemsStack.Count != 0)
{
TreeViewItem current = treeViewItemsStack.Pop();
current.IsExpanded = false;
for (int i = 0; i < current.Items.Count; i++)
{
treeViewItemsStack.Push(current.ItemContainerGenerator.ContainerFromIndex(i) as TreeViewItem);
}
}
}


That's it we are done, now run your application to see the Expand All and Collapse All Buttons in action.

ima7.gif

Enjoy Coding.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor