What's important here is that the last level displayed in your TreeView is expressed in a listed first in your Resources section, as shown in lines 14-16 of Figure 4. With the ItemsSource property of the TreeView control set to the collection of EmployeeType objects, you are now ready to start building the hierarchy of XAML DataTemplate objects. Notice in line 28 of Figure 4 that the ItemsSource of the TreeView control is using the static resource with the key of theData as the source of data for the TreeView.įigure 4: You need to start from the bottom up when laying out your templates for a TreeView Provide your instance of EmployeeTypes with a name using the x:Key attribute, in this example, theData. Instead, you use XAML to create the instance of the EmployeeTypes class as shown in line 12 of Figure 4. When using XAML, you don't need to create an instance of the EmployeeTypes class in C# or Visual Basic code. Matching your class hierarchy to the TreeView display is your first step in ensuring that you can display your data in a TreeView format.įigure 3: A hierarchical data structure of EmployeeTypes containing a collection of Employee objects Create Your Data and Data Templates Using XAML At the top, you have the EmployeeTypes ( Manager and Project Managers), and the next level under each EmployeeType is a collection of Employee objects ( Tim, John, David). If you compare Figure 1 and Figure 3, you can see that the two structures are very similar. EmployeeTypes types = new EmployeeTypes() If you create an instance of the EmployeeTypes class as shown in the following code snippet, you will create the hierarchy of EmployeeTypes and Employees shown in Figure 3. Type = new EmployeeType("Project Managers") Listing 1: A collection class is a great place to initialize default data for that collection. Feel free to load this collection with the data of your choice. For purposes of this article, I'm using hard-coded data. In the constructor of the EmployeeTypes collection class, you build the collection of EmployeeType objects and fill each one with Employee objects, as shown in Listing 1. This is a collection of EmployeeType objects. The last class you create is a collection class called EmployeeTypes created using the generic List class. First is a string property, called Description, which holds the name of the EmployeeType, such as “Supervisor” or “Project Manager.” The second property, Employees, is a generic collection property to hold a list of Employee objects that are associated with this particular EmployeeType. The EmployeeType class contains two properties. A constructor is created to accept a name argument that you can use to set the Name property when you create an Employee object. This class has a single property called Name. The first class you build is the Employee class. Just as a TreeView is a hierarchical structure, create a set of classes that is also a hierarchy.įigure 2 shows the three classes that will be necessary for expressing a hierarchy of EmployeeType objects of which each EmployeeType object can hold one or more Employee objects.įigure 2: Three classes are needed to express a three-level TreeView. What's important is how you structure the classes into which your data will be placed. How and from where you retrieve the data is not important for this article. You want to retrieve data from a database table or an XML file and build the TreeView dynamically. You don't want to use hard-coded XAML in your WPF application. įigure 1 shows you how this TreeView looks when you run the WPF application.įigure 1: A hard-coded two-level TreeView Create Hierarchical Classes to Mimic Your TreeView To start, let's look at the typical two-level WPF TreeView that has been hard-coded with the values shown in the following code snippet. In this article, I'm going to break down how these templates work, so you can really understand what's going on underneath the hood. What if you have more than two levels? This is where it's vital to understand exactly how the HierarchicalDataTemplates work. There are many examples of using the Windows Presentation Foundataion (WPF) TreeView that you can find on the Web, however, most of them only show you how to go to two levels.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |