A Sample and a Question
This month, I’m asking for your input to this question: Which type of sample do you prefer, simple and straightforward, or complex with a more real-world scenario? We often hear that our customers want real-world examples, and I’d like to understand at what level they want the code to be “real-world?”
I recently wrote a topic describing how to create a TreeView with an unknown depth, and I used very simple code to illustrate the concept. Here’s the interesting parts of the topic:
Bind a TreeView to Data that has an Unknown Depth
There might be times when you want to bind a TreeView to a data source whose depth is not known. This can occur when the data is recursive in nature, such as a file system, where folders can contain folders, or a company's organizational structure, where employees have other employees as direct reports.
The data source must have a hierarchical object model. For example, an Employee class might contain a collection of Employee objects that are the direct reports of an employee. If the data is represented in a way that is not hierarchical, you must build a hierarchical representation of the data.
When you set the ItemsControl.ItemTemplate property and if the ItemsControl generates an ItemsControl for each child item, then the child ItemsControl uses the same ItemTemplate as the parent. For example, if you set the ItemTemplate property on a data-bound TreeView, each TreeViewItem that is generated uses the DataTemplate that was assigned to the ItemTemplate property of the TreeView.
The HierarchicalDataTemplate enables you to specify the ItemsSource for a TreeViewItem, or any HeaderedItemsControl, on the data template. When you set the HierarchicalDataTemplate.ItemsSource property, that value is used when the HierarchicalDataTemplate is applied. By using a HierarchicalDataTemplate, you can recursively set the ItemsSource for each TreeViewItem in the TreeView.
<Page
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<XmlDataProvider x:Key="myCompany" XPath="Company/Employee">
<x:XData>
<Company xmlns="">
<Employee Name="Don Hall">
<Employee Name="Alice Ciccu">
<Employee Name="David Pelton">
<Employee Name="Vivian Atlas"/>
</Employee>
<Employee Name="Jeff Price"/>
<Employee Name="Andy Jacobs"/>
</Employee>
<Employee Name="Bill Malone">
<Employee Name="Maurice Taylor"/>
<Employee Name="Sunil Uppal"/>
<Employee Name="Qiang Wang"/>
</Employee>
</Employee>
</Company>
</x:XData>
</XmlDataProvider>
<!-- Bind the HierarchicalDataTemplate.ItemSource property to the employees under
each Employee element. -->
<HierarchicalDataTemplate x:Key="EmployeeTemplate"
ItemsSource="{Binding XPath=Employee}">
<TextBlock Text="{Binding XPath=@Name}" ></TextBlock>
</HierarchicalDataTemplate>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="True"/>
</Style>
</Page.Resources>
<Grid>
<TreeView ItemsSource="{Binding Source={StaticResource myCompany}}"
ItemTemplate="{StaticResource EmployeeTemplate}"/>
</Grid>
</Page>
I think this nicely illustrates the concept, but you can hardly call it a real-world example. A more real-world example is creating a TreeView that mirrors the file structure of a Disk Drive. Kevin Moore’s Bag-O-Tricks included such a control, and we certainly could include a sample that does the same, but such a sample will be more complex than just showing how to make the TreeView recurse an unknown level. If I used Kevin Moore’s code, I couldn’t even show the entire XAML. The most that would make sense to show is HierarchicalDataTemplate, and then refer people to the sample for the entire sample.
<HierarchicalDataTemplate x:Key="DirectoryStyleForTreeView"
DataType = "{x:Type fp:SelectableDirectory}"
ItemsSource = "{Binding Path=SubDirectories}">
<StackPanel Orientation="Horizontal">
<CheckBox Name="checkBox" IsChecked="{Binding Path=IsSelected}" Focusable="false">
<DockPanel x:Name="panel">
<Control x:Name="icon" Template="{StaticResource folder}"/>
<TextBlock Text="{Binding Path=Name}" Margin="0 1.5 0 0"/>
</DockPanel>
</CheckBox>
</StackPanel>
</HierarchicalDataTemplate>
I’ve attached the sample, so you can look at the entire application and see the HierarchicalDataTemplate in context of the program.
I’ve attached both samples for you to compare the complexity versus the real-world use value. My thought is the Folder Picker sample is a lot less self-contained and takes a lot more time to investigate how to populate a TreeView for an unknown depth, which is why I used the simpler sample to explain this concept. My question is which one do you prefer? I know your immediate reaction might be to say, “Hey, why don’t you give us both?” That’s definitely ideal, but for the sake of argument, let’s assume only one is allowed. Is the simpler one adequate, or is it worth your time (and ours) to provide the more complex sample and let you experiment with it to extract the information you need?
Comments
Anonymous
January 29, 2008
PingBack from http://msdnrss.thecoderblogs.com/2008/01/29/a-sample-and-a-question/Anonymous
January 29, 2008
It depends what you are trying to do. One needs a basic understanding of a particular technique or an API in order to understand a more complex sample. And usually people don't want to spend a lot of time learning the basics, so for this a simple sample is best. Once you know the basics expert insight is always valuable when you start to implement the technique in a real-life app. But a complex, real-life scenario is never quick to absorb. If "both" is not an option, try to at least provide some tidbits. For a simpler sample provide some insight at the end in the lines of "For a real-life scenario is best if you..." etc. In case of a complex sample, at the start of the sample provide links to the docs (MSDN or elsewhere) that explain the basics. Also, make clear which of the two each article is about. PS. I'm not too fond of XmlDataProvider as the data source in the samples. I know it allows for compact code samples, but I'm always left with the question, how do do the same with a CLR object. Here you could provide both (ie. class Company).Anonymous
January 30, 2008
Another "real world" example of the HierarchicalDataTemplate is for menu's that get their data from SQL Server. By having a column with the items parent, the HierarchicalDataTemplate makes this task of multi level menus a snap. If the articles are SUPER deep, you run the risk of viewers not getting anything from it. So a middle of the road approach may be best. Or provide a series that takes you brick by brick. Request: Can we get examples of using control designers for our WPF controls. Which attributes do we use, what are the control designer classes support by VS2008 and Blend. How do we write our own control designers like we do for ASP.NET and Winforms. Thank you and have a great day. Karl ShifflettAnonymous
January 31, 2008
I tend to prefer the simplest example that provides enough context to fully illustrate the idea. The problem with fuller, real-world samples is it becomes difficult to extract the essence from all the extra stuff required to fill the sample out. Also, when it makes sense, a simple example that shows both the XAML way and the coding way to do something is best, because for some of us XAML is not an option. Thanks, EricAnonymous
February 02, 2008
I would love to see the source code or guidance on creating standard Windows look-and-feel Document-View applications (MDI). It has to be a standard Windows UI business application without 3D-gradients and bells and whistles. E.g. how to build a toolbar that looks like an Office 2003 toolbar (not 2007!). MFC had that, and I can't find a WPF alternative. Basically, I'd like to see a sample of a WPF application with the look, feel and functionality of an MDI Wordpad. Thanks! KirillAnonymous
February 24, 2008
The comment has been removedAnonymous
February 28, 2008
I think simple samples are the better way for blog-posts and presentations. An additional complex sample would always be great.