Exploring Creativity with Design / Graphics / Technology
Automatic Form Layouts With the TreeView
Over the past couple of days I have been working on an application that
automatically generates a Form layout from an XML file. The XML file
represents a form with a bunch of parameters. The form can also contain
parameter groups, which can encapsulate a set of parameters. Effectively
the XML structure is something like this:
The ParameterGroup has attributes like Collapsible (True, False), Orientation (Horizontal, Vertical). The Parameter can be of different datatypes specified by the Type property. Both the ParameterGroup and Parameter have a Name property.
A TreeView for a form layout
The original XML file had different naming conventions for these
elements. I did a simple Linq to XML translation to create an object
model with the type names as seen in the figure above. With that said,
here is a sample form layout:
The bold arrows indicate the Orientation of the ParameterGroup. The
rectangles are the containers for the groups and for the form itself.
From the description so far it should be evident that we are dealing
with a hierarchical structure in the form layout. With a little stretch
of the mind it is not difficult to imagine that the layout is an
instance of a TreeView!
Styling the TreeView to look like a Form
The TreeView is composed of TreeViewItems and HierarchicalDataTemplates.
By using specialized ControlTemplates for the TreeViewItem and combining
those with the HierarchicalDataTemplates for the ParameterGroup and
Parameter, it is possible to make the TreeView look like a form. Josh
Smith wrote a good article on CodeProject
to give you a head start for styling TreeViews.
Here is my version of the TreeView templates for the form layout.