Analysis of application of WPF binding hierarchy Data _ practical skills

Source: Internet
Author: User

There are multiple types of hierarchical data in the actual project application, and WPF provides a good mechanism for data binding. The most frequently used is the ListBox and TreeView controls.

The difference between a ListBox and a TreeView control
1.ListBox display single level data collection, TreeView can display single level and multi-level data collection;
2. Through the listbox at the UI level can display good data display effect, the data collection can be sorted, grouped, filtering operations;
3.TreeView Display as a hierarchical data collection for the tree structure, through the Templete and style properties can also define good data display effect;

Ii. ListBox Control Example
1.ListBox binding Data for grouping:
Use the Listbox.gridstyle tab to define the appearance of the group header by defining the HeaderTemplate property:

Copy Code code as follows:

<listbox itemsource= "{Binding path=data}" >
<image source= "Xxx.jpg"/>
<label content= "C:"/>

This enables you to create an effect similar to the Windows File Manager:

2.Listbox Some experience in using:
/1 If you want to achieve a nice interface effect like Windows and group, you need to customize the Groupstyle style, otherwise WPF will use the built-in Groupstyle, or you can reference groupstyle.default static properties.
/2 The ListBox can only define a single layer of data structure, use the listbox again in the item in the ListBox, the Itemsource in the listbox will not inherit the data collection from the item source of the previous ListBox, such as the following data collection:
Copy Code code as follows:

Public list<groups> Groups = new list<groups> (); Groups. ADD (new Group);.

Copy Code code as follows:

public class Group {
public int Id {get; set;}
public string Name {get; set;}
Private list<box> boxes = new list<box> ();
Public list<box> Boxes {
get {return boxes;}

The listbox's Itemsource Binding the List<groups> data collection, and the listbox in its item Binding List<box> The listbox in item is unable to get the List <Box> of this data collection;

Three, TreeView control examples
1. Like a collection of data, use the TreeView to bind a multi-tiered collection of data:
Copy Code code as follows:

<treeview x:name= "Maintree" focusvisualstyle= "{x:null}" itemssource= "{Binding Groups}" >
<style targettype= "{x:type TreeViewItem}" >
<setter property= "isexpanded" value= "{Binding isexpanded, mode=twoway}"/>
<setter property= "isselected" value= "{Binding isselected, mode=twoway}"/>
<setter property= "FontWeight" value= "Normal"/>
<trigger property= "isselected" value= "True" >
<setter property= "FontWeight" value= "Bold"/>
<stackpanel orientation= "Horizontal" >
<label content= "{Binding path=friendlyname}" ></Label>
<checkbox verticalalignment= "Center" ischecked= "{Binding path=isselected}" ></CheckBox>

<datatemplate datatype= "{x:type M:boxvo}" >
<grid margin= "0,5,5,10" mousedown= "Maintree_mousedown" loaded= "grid_loaded" >
<columndefinition width= "*" ></ColumnDefinition>
<columndefinition width= "6*" ></ColumnDefinition>
<image source= "/resources/images/shgbit.png" width= "verticalalignment=" "Top" grid.column= "0" Grid.Row= "0" > </Image>
<label grid.rowspan= "2" grid.row= "0" grid.column= "0" margin= "5,5,0,0" content= "{Binding path=friendlyname}" > </Label>

The Hierarchicaldatatemplate property is a hierarchical data template that inherits the hierarchy of the data set, and it must use hierarchicaldatatemplate to represent the hierarchical dependencies of the tree.
Property binding Data Using TwoWay is a two-way property, when the source data or target is changed to update the other party's data. A typical application in a hierarchical tree representation is to use a checkbox for the selection of child nodes and the unchecked state delivery.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.