Get the App
SLTechnology News&Howtos  ›  Development  › 

How WPF binds hierarchical data

Shulou Source: shulou.com Published: 2022-06-03 05:56:37 10月03日 Update

This article is about how WPF binds hierarchical data. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

There are many types of hierarchical data in practical project applications, and WPF provides a good data binding mechanism. Among them, ListBox and TreeView controls are used most frequently.

I. the difference between ListBox and TreeView controls

1.ListBox displays single-level data sets, while TreeView can display single-level and multi-level data sets.

two。 The good data display effect can be displayed at the UI level through ListBox, and the data sets can be sorted, grouped and filtered.

3.TreeView is displayed as a multi-level data set with a tree structure, and its well-defined data display effect can also be defined through Templete and Style properties.

2. ListBox control example

1.ListBox binding data is grouped:

Using the ListBox.GridStyle tag, define the HeaderTemplate attribute that defines the appearance of the group header:

The code is as follows:

Code

.

This creates an effect similar to that of the WINDOWS file manager:

Some experience with 2.Listbox:

/ 1 if you want to achieve a beautiful interface similar to WINDOWS and group it, you need to customize the style of GroupStyle, otherwise WPF will use the built-in GroupStyle, or you can reference the static property of GroupStyle.Default.

/ 2 ListBox can only define one layer of data structure. If ListBox is used again in Item in ListBox, the ItemSource in ListBox will not inherit the data set from the Item source of the upper layer ListBox, such as the following data set:

The copy code is as follows:

Public List groups = new List (); groups.Add (new Group);.

The copy code is as follows:

Public class Group {

Public int Id {get; set;}

Public string Name {get; set;}

Private List boxes = new List ()

Public List Boxes {

Get {return boxes;}

}

}

The data set of Listbox's ItemSource Binding List, and the ListBox Binding List in its Item, then the ListBox in Item cannot obtain the data set of List.

Third, TreeView control example

1. As with the data set above, use TreeView to bind multi-tier data sets:

The copy code is as follows:

Code

The HierarchicalDataTemplate attribute is a hierarchical data template, which inherits the hierarchical structure of the data collection, and HierarchicalDataTemplate must be used to represent the hierarchical dependencies of the tree.

Attribute binding data using TwoWay is a bi-directional property, when the source data or target is changed to update the other party's data. The typical application in hierarchical tree representation is to use CheckBox to select child nodes and transfer unselected states.

Thank you for reading! This is the end of the article on "how to bind hierarchical data in WPF". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Data code structure properties hierarchy controls effects good multi-tier hierarchical grouping content single-tier multi-level that is more examples articles applications good Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Xiaomi MariaDB vpn Microsoft