Use the TreeView control
The TreeView control can display a hierarchical (or nested, or recursive) collection of data in a tree of expandable nodes.
For more information about the TreeView control, see TreeView Class (System.Windows.Controls) on MSDN.
Create sample data to display in a TreeView control
You can use any type of collection in the TreeView control, but hierarchical collections make full use of the functionality of the TreeView control. If you don't already have data that you can use in the procedure "To populate a TreeView control," later in this topic, follow this procedure to set up sample data that will fully illustrate the features of the TreeView control:
To create sample data to display in a TreeView control
If you do not already see the Data panel, click the Window menu, and then click Data.
In the Data panel, click Create sample data , and then click New Sample Data.
In the New Sample Data dialog box, you can change the default name for the data set, and then select the part of your project to which you want the data made available.
Select the Enable sample data when application is running box.
Tip
The Enable sample data when application is running box lets your project display the sample data when you run your application. When the box is not selected, Blend for Visual Studio 2012 will display the sample data only on the artboard.
For more information, see Make your application display sample data when it runs.
Click OK.
A sample data source that includes a collection of two types of properties (string and Boolean ) is created.
Next to Collection, click the drop-down arrow next to Add simple property , and then click Convert to Hierarchical Collection.
When a flat collection is converted to nested collections, the collection provides properties that are nested five levels deep. The name of the second-level collection becomes editable.
You now have a sample data collection ready to display in a TreeView control.
For information about modifying sample data and adding more data properties, see Modify sample data.
Populate the TreeView control
There are multiple ways to bind data to a control on the artboard. The following procedure shows you the fastest way when you are using a TreeView control.
For other options, see Bind an object to data.
To populate the TreeView control
In the Tools panel, click Assets .
Tip
Alternatively, you can display the Assets panel by clicking Assets on the Window menu.
In the Assets panel, expand Controls, click All, and then click the TreeView control .
The button underneath the Assets button in the Tools panel displays the icon for the TreeView control and is selected.
Using your pointer, draw a large TreeView object on the artboard in your main document.
In the Data panel, drag the top-level collection ( Collection) onto the new TreeView object.
The TreeView object displays the collection in the sample data source, converting image properties to image objects, and Boolean properties to check boxes.
Press F5 to run your application.
If you are using the sample data from the previous procedure, and you enabled the sample data when testing your application, you should see the following:
Change the appearance of the TreeView control
You can change the appearances of controls by modifying their properties or by modifying the templates that are used to style them. You can modify the overall template, the templates that arrange items in each row, and others.
For more information, see Create or modify a template.
To change the layout and appearance of items in each row
In the Objects and Timeline panel, or on the artboard, right-click the TreeView object, point to Edit Additional Templates, point to Edit Generated Items (ItemTemplate), and then click Edit a Copy.
In the Create Data Template Resource dialog box, click OK.
Blend enters template-editing mode. The breadcrumb bar at the top of the artboard shows that you are editing a template inside the TreeView object. You can use the breadcrumb bar to exit template-editing mode by clicking the TreeView button.
In the Objects and Timeline panel, you can see that items are arranged in a StackPanel object. You can change the stack orientation and the stacking order. You can also change the StackPanel to a different layout panel, such as the Grid or Canvas layout panel.
For more information, see the following topics:
In the Objects and Timeline panel, add other objects to the template, or modify the properties of existing objects in the Properties panel.
To change the color of the selected item
In the Objects and Timeline panel, or on the artboard, right-click the TreeView object, point to Edit Additional Templates, point to Edit Generated Item Container (ItemContainerStyle), and then click Edit a Copy.
In the Create Style Resource dialog box, click OK.
Blend enters template-editing mode. The breadcrumb bar at the top of the artboard shows that you are editing a template inside the TreeView object. You can use the breadcrumb bar to exit template-editing mode by clicking the TreeView button.
Tip
When you first enter template-editing mode, the appearance of your template reflects the appearance of the control while there is no user interaction with the control. To modify the appearance of the control when a user interacts with it, you have to select a trigger (in Windows Presentation Foundation [WPF] projects) or a state (in Microsoft Silverlight projects) while in template-editing mode. For example, when users select an item in a TreeView control, they are interacting with the control, and causing a Selected event to occur. To modify the appearance of the control when an item is selected, you have to modify the template while the trigger or state that corresponds to the Selected event is selected.
Do one of the following:
For a WPF project: In the Triggers panel, click the IsSelected = True trigger to enter template-editing mode for the Selected event. In the Objects and Timeline panel, click the Bd object. In the Properties panel, under Brushes, change the Background color of the Bd object. When you are done, click Default in the Triggers panel to turn off trigger recording.
For a Silverlight project: In the States panel, click the Selected state to enter template-editing mode for the Selected event. In the Objects and Timeline panel, click the Selection object. In the Properties panel, under Brushes, change the Fill and Stroke colors of the Selection object. When you are done, click Base in the States panel to turn off state recording.
For information about opening panels, see Add or remove a workspace panel.
For information about changing brush colors, see Setting colors, brushes, and masks.
Press F5 to run your application and select an item in the tree.
To change the appearance of the expand button
In the Objects and Timeline panel, or on the artboard, right-click the TreeView object, point to Edit Additional Templates, point to Edit Generated Item Container (ItemContainerStyle), and then click Edit a Copy.
Tip
If you have already created this template, the Edit Current command will be enabled, and you can select that instead of Edit a Copy.
In the Create Style Resource dialog box, click OK.
Blend enters template-editing mode. The breadcrumb bar at the top of the artboard shows that you are editing a template inside the TreeView object. You can use the breadcrumb bar to exit template-editing mode by clicking the [TreeView] button.
In the Objects and Timeline panel, right-click the Expander object (WPF projects) or the ExpandButton object (Silverlight projects), point to Edit Template, and then click Edit Current.
In the Objects and Timeline panel, expand the nodes to see the Path object that represents the expand button for the TreeView control. In WPF projects, the path object is named ExpandPath. In Silverlight projects, there are two path objects, named UncheckedVisual and CheckedVisual. You can use the Pen and Direct Selection tools to modify the shapes of these paths. In WPF projects, the name and type of the ExpandPath object must be preserved because there are triggers that depend on it.
For more information, see Triggers.
In Silverlight projects, you can change the appearance of the path objects changes in the Checked state, so you can modify that change.
For more information, see Defining different visual states for a control.