WPF Reflections

Jan 24 2008   10:01AM GMT

Hierarchial data template

Mark Shurmer Profile: MarkWPF

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}” />

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

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: