We mentioned in the previous section that multiple attributes of CellRenderer can be bound to ListStore. In this way, we can not only specify the text to be displayed in the data Model, but also specify the font, foreground color, background color, and so on of the cell. In this way, we can customize a more beautiful interface.
Here we will demonstrate how to change the foreground color of a cell (that is, the font color), which is actually very simple.
First, we change the ListStore object so that it can store the color information we need.
1 ListStore musicList = new ListStore (typeof (string), typeof (string ));
2 musicList. AppendValues ("beyond", "red", "sea sky", "blue ");
3 musicList. AppendValues ("Yuquan", "red", "Rainbow", "blue ");
Our musicList has four columns, of which column 2 and 4 store the color of cells.
Add an attribute ing.
1 TreeViewColumn artistCol = new TreeViewColumn ();
2 artistCol. Title = "Artist ";
3 CellRendererText artistNameCell = new CellRendererText ();
4 artistCol. PackStart (artistNameCell, true );
5 artistCol. addattriell (artistNameCell, "text", 0 );
6 artistCol. addattriell (artistNameCell, "foreground", 1 );
Row 6 adds a foring of the Foreground Property (Property) of CellRendererText.
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 = 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 artistCol. addattriell (artistNameCell, "foreground", 1 );
27
28
29 TreeViewColumn songCol = new TreeViewColumn ();
30 songCol. Title = "Song ";
31 CellRendererText songTitleCell = new CellRendererText ();
32 songCol. PackStart (songTitleCell, true );
33 songCol. addattricell (songTitleCell, "text", 2 );
34 songCol. addattricell (songTitleCell, "foreground", 3 );
35
36 tree. AppendColumn (artistCol );
37 tree. AppendColumn (songCol );
38
39 ListStore musicList = new ListStore (typeof (string), typeof (string ));
40 musicList. AppendValues ("beyond", "red", "sea sky", "blue ");
41 musicList. AppendValues ("Yuquan", "red", "Rainbow", "blue ");
42
43 tree. Model = musicList;
44 window. Add (tree );
45 window. ShowAll ();
46}
47
48 private void Delete (object o, DeleteEventArgs args)
49 {
50 Application. Quit ();
51}
52}
Running result