Auto expanding tree view for Silverlight

by StefanOlson 14. May 2009 12:30

Bea Stollnitz has been running a series on her blog, describing how to expand items in the tree view, in both WPF and Silverlight, but much of it was extraordinarily complicated.   For the project I'm working on with Silverlight and the .net RIA services I needed one of my tree views to appear completely expanded initially.  There's a lot of code on Bea’s blog, but I cut down what I needed to this new tree view control.  You can use it in your application and your tree will initially be completely expanded:

public class AutoExpandingTreeViewItem : TreeViewItem
{
    protected override DependencyObject GetContainerForItemOverride()
    {
        return new AutoExpandingTreeViewItem { IsExpanded = true };
    }
}
public class AutoExpandingTreeView : TreeView
{
    protected override DependencyObject GetContainerForItemOverride()
    {
        return new AutoExpandingTreeViewItem { IsExpanded = true };
    }
}

Here's how you would use it in your page:

<UserControl x:Class="HireMyStuffAdmin.SilverlightControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:toolkit="clr-namespace:System.Windows;assembly=System.Windows.Controls" 
    xmlns:controls="clr-namespace:OlsonSoftware.controls" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <controls:AutoExpandingTreeView ItemsSource="{Binding Categories}">
            <controls:AutoExpandingTreeView.ItemTemplate>
                <toolkit:HierarchicalDataTemplate  ItemsSource="{Binding ChildCategories}">
                    <StackPanel >
                        <TextBlock Text="{Binding Name}"></TextBlock>
                    </StackPanel>
                </toolkit:HierarchicalDataTemplate>
            </controls:AutoExpandingTreeView.ItemTemplate>
        </controls:AutoExpandingTreeView>
    </Grid>
</UserControl>

Tags:

Silverlight

Comments

5/4/2010 5:03:58 PM #

Pingback from endyear2012.com

car insurance cheap quote «  End Year 2012

endyear2012.com

Add comment




biuquote
  • Comment
  • Preview
Loading



About the author

Stefan Olson is the Managing Director of Olson Software.  He has been developing software using Microsoft Technologies for nearly 20 years.

He is currently working on building the next generation Virtual Tour software in WPF and Silverlight for www.palacevirtualtours.com.

Tag cloud