Skip to content

Instantly share code, notes, and snippets.

@jsonw23
Created February 27, 2012 21:32
Show Gist options
  • Save jsonw23/1927238 to your computer and use it in GitHub Desktop.
Save jsonw23/1927238 to your computer and use it in GitHub Desktop.
Folder Tree WPF Control: Part 1 - Lazy Loading
<TreeView ItemsSource="{Binding Path=Drives}" TreeViewItem.Expanded="FolderTree_Expanded">
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="{Binding Path=IsExpanded, Mode=TwoWay}" />
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Path=Folders}">
<TextBlock Text="{Binding Path=Label}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
public partial class FolderTree : UserControl
{
...
private void FolderTree_Expanded(object sender, RoutedEventArgs e)
{
var treeViewItem = (TreeViewItem)e.OriginalSource;
var node = (FolderTreeItem)treeViewItem.Header;
node.LoadChildren();
}
}
namespace GeekJ.FolderTreeControl.Model
{
public abstract class FolderTreeItem : ViewModelBase
{
private bool _isExpanded;
public bool IsExpanded
{
get
{
return _isExpanded;
}
set
{
_isExpanded = value;
OnPropertyChanged("IsExpanded");
}
}
public abstract void LoadChildren();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment