WPF Reflections

Jan 24 2008   10:01AM GMT

Hierarchial data template



Posted by: MarkWPF
Tags:
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

 
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.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

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: