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="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://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?