[Mono learning] Tree View Treeview learning notes (2): add data

Source: Internet
Author: User

The example in the previous section only demonstrates the basic framework of the TreeView program and does not include any data. In this example, we need to add our own data. First, should the data be added to that object?

 

By the way, it is the ListStore Class Object musicList. We want to add data to it.

The following code adds two rows of data:

Musiclist. appendvalues ("beyond", "sea sky ");
Musiclist. appendvalues ("Yuquan", "Rainbow ");

 

Tips

Method AppendValues:

Prototype: Public treeiter appendvalues (array values)

Function: Insert a row of data into the model. The data is input as an array. They must be consistent with the specified type and number of parameters when declared by liststore. Like the musiclist object declared in the previous section, each row of the object contains two string objects.

Liststore musiclist = new liststore (typeof (string), typeof (string ));

 

However, the work is not over yet. As we mentioned in the previous section, TreeViewColumn does not know how to render data. It uses the CellRenderer class. Therefore, we need to add a CellRenderer object to it and use this object to render cells ). In addition, TreeViewColumn does not know which Column of the Model musicList (Model) should be bound to the attribute of the CellRenderer object, we need to add features to the TreeViewColumn to tell the CellRenderer how to bind data.

The Code is as follows:

Cellrenderertext songtitlecell = new cellrenderertext ();
Songcol. packstart (songtitlecell, true );
Songcol. addattricell (songtitlecell, "text", 1 );

Code explanation:

1. the columns in the ListStore and the columns in the List View in the TreeView do not have any relationship. Their names may cause misunderstanding, but remember that they are not in any contact. In fact, a column in the data Model corresponds to an attribute of the CellRenderer object.

2. The above section of code becomes a songTitleCell object and loads it into the TreeViewColumn. Then we add an Attribute to the CellRenderer object we just added ). This feature binds the data in the second column of the musicList (the serial number of the ListStore column starts from 0 like the C language) to the Text attribute of the CellRendererText object. The Text attribute of CellRendererText is the Text displayed in cells.

3. Each cellrenderer has many attributes. For example, in the cellrenderertext class, its text attribute specifies the displayed text. In addition, it also has a foregroud attribute that specifies the foreground color of the cell. You can set this attribute if you want to change the font color.

4. You can specify the values of multiple attributes of a cellrender at the same time.

Tips

PackStart method:

Prototype: Public void packstart (cellrenderer cell, bool expand)

Function: Add cellrenderer to treeviewcolumn from start to end.

The first parameter specifies the cellrenderer to be added;

The second parameter specifies the arrangement of the added cells. If false, each cell occupies only the space required by itself. If true, the remaining space is evenly allocated.

Tips

Addattivity method:

Prototype: public void addatti.pdf (CellRenderer cell, string attribute, int column );

Function: Set Properties for CellRenderer in TreeViewColumn.

The first parameter is the CellRenderer of the feature to be added;

The second parameter is to bind a value to the Property of the CellRenderer. A CellRenderer may have many attributes. Here, it is better to write the attribute name in lower case as Text.

The third is that the bound data source is the column of the data Model (whose serial number starts from 0 ).

 

The complete code is as follows:

1 using system;
2 using GTK;
3
4 public class treeviewexample
5 {
6 public static void main (string [] argv)
7 {
8 application. INIT ();
9 new TreeViewExample ();
10 Application. Run ();
11}
12
13 public TreeViewExample ()
14 {
15 Window window = new Window ("TreeView Example ");
16 window. SetSizeRequest (500,200 );
17 window. DeleteEvent + = Delete;
18
19 TreeView tree = new TreeView ();
20
21 treeviewcolumn artistcol = new treeviewcolumn ();
22 artistcol. Title = "artist ";
23 cellrenderertext artistnamecell = new cellrenderertext ();
24 artistcol. packstart (artistnamecell, true );
25 artistcol. addattriell (artistnamecell, "text", 0 );
26
27 treeviewcolumn songcol = new treeviewcolumn ();
28 songcol. Title = "song ";
29 CellRendererText songTitleCell = new CellRendererText ();
30 songCol. PackStart (songTitleCell, true );
31 songCol. addattricell (songTitleCell, "text", 1 );
32
33 tree. AppendColumn (artistCol );
34 tree. AppendColumn (songCol );
35
36 ListStore musicList = new ListStore (typeof (string), typeof (string ));
37 musicList. AppendValues ("beyond", "sea sky ");
38 musiclist. appendvalues ("Yuquan", "Rainbow ");
39
40 tree. Model = musiclist;
41 window. Add (tree );
42 window. showall ();
43}
44
45 private void Delete (Object o, deleteeventargs ARGs)
46 {
47 application. Quit ();
48}
49}

 

Compile:

MCS-PKG: gtk-sharp-2.0 treeviewexample. CS

 

Run:

Mono treeviewexample.exe

 

Running result:

 

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: info-contact@alibabacloud.com 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.