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>
9fe1d6dc-0fc0-46d7-97e7-ea8616a32c8a|0|.0
Tags:
Silverlight