WPF Reflections

Jan 24 2008   10:01AM GMT

Hierarchial data template



Posted by: Mark Shurmer
WPF

Although you can do recursion quite simply with DataTemplates (see recursion example), you may want to use a Hierarchical Data Template instead. You may want to do this if you are for example connecting the Data Template up to a TreeView.

The template will look like this for a tree of files and directories:

<HierarchicalDataTemplate DataType=”{x:Type si:DirectoryInfo} ItemsSource=”{Binding Path=., Converter={StaticResource fileConv}}”>
<TextBlock Text=”{Binding Path=Name}” />
</HierarchicalDataTemplate>

Where fileConv is a static resource to a converter class that converts a FileInfo into a string, and si refers to System.IO.

The magic happens in the ItemSource property, which when it encounters a DirectoryInfo will create another template, and so on.

Comment on this Post


You must be logged-in to post a comment. Log-in/Register