![]() But a little-known trick in Windows Presentation Framework (WPF) is lazy loading, which gives you the ability to load data on demand quickly and without using expensive resources to hold all the data. ![]() a large amount of data by the default procedure in our WPF TreeView control can take time and consume many resources. creating auxiliary datatable for iteratively querying // in the subsequent levels of hierarchy Where itemSub.Field( " level") = level - 1 select new ItemsInputDT.Field( " GparentId") equals itemSub.Field( " Gid") Public void NodesFactory(DataTable InptDT, DataTable subDT, long level)įrom itemsInputDT in InptDT.AsEnumerable() Public partial class MainWindow : Window, INotifyPropert圜hanged At the end, TreeViewModel object is bound with datatreeView component." List nodesList" list is created to hold NodeViewModel objects to the next iteration in which their children collection will be added. In the current iteration, created nodes are added to the children collection field of the nodes created in previous iteration. This field will be filled in the next iteration. These nodes are created with empty Children field first. During single iteration, using foreach loop, the NodeViewModel objects are created for all items within specific level. In each individual iteration, one hierarchy level is processed. Using recursive function, LINQ statement, joining with " sub" datatable and assigning relevant level of hierarchy and then filtering by relevant level ," InputDT" datatable is iteratively queried to find all items for specific level of hierarchy. Joining with " sub" datatable and filtering the relevant hierarchy level number makes it possible to find children of the nodes created in the previous iteration. Further processing takes place inside the " NodesFactory" function. The results are loaded to " sub" datatable object and at the same time, the level of hierarchy number 0 is assigned. First, the " InputDT" datatable object is queried using LINQ statement to find the root item.Overall Description of How the Code Works This function contains the entire logic to step through all the levels of hierarchy iteratively to find collections of children and create items of the TreeViewModel. " NodesFactory" is a recursive function that accepts three parameters: " InputDT" datatable, " sub" datatable, level number.The NodeViewModel object contains the following 4 fields: The element ID (numeric type), element name (string types), expand property (bool type, to define whether node should be expanded or collapsed at the start) and the fourth field is the collection of children of the item. TreeViewModel contains " Items" field to hold the collection of NodeViewModel objects. There are two special classes to define nodes of the tree: TreeViewModel and NodeViewModel.It allows to select only a specific level of hierarchy in the relevant iteration. " sub" datatable is an auxiliary object with similar structure as " InputDT" datatable, but contains additional " Level" column. During single iteration, " InputDT" datatable is joined with " sub" datatable using LINQ statement to select rows with children for specific level of hierarchy in the subsequent iterations. " InputDT" datatable object reflects the same structure as the " GIER_Hierarchy_List" list and is used to process the data in memory.In the next step, all data from this generic list will be loaded into " InputDT" datatable object using foreach statement.Let's assume that " GIER_Hierarchy_List" generic list is database table and therefore its contents will be loaded to datatable object for further processing in memory. It will be our source of data to populate TreeView. GIER_Hierarchy class and the GIER_Hierarchy_List generic list were created only to define some examples of hierarchical data, in this case, fictional organizational company structure. ![]() ![]() Using the Code Description of Declared Classes and Objects The main goal was to create the TreeView UI using basic, fully controllable statements. To simplify the example, the database and relevant entity framework's classes were replaced by generic list. The presented code was written to fill TreeView UI with the hierarchical data from database. Each parent can have numerous children, but a child can have only one parent. ![]() The code supports the parent/child hierarchy with one root. The source of data is a self-referencing collection of items with the following fields: Id of Item, Name of Item, Id of parent. It steps through all the levels of hierarchy, from top to bottom and adds to each item a collection of its children. The code fills WPF TreeView with a multilevel hierarchical data. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |