我们在上一节提到可以把CellRenderer的多个属性都绑定到ListStore上面。这样我们在数据模型(Model)里面不但可以指定要显示的文本,还可以指定单元格的字体,前景色,背景色等等,通过这样定制出更美观的界面。
在这里我们演示一下如何改变单元格的前景色(就是字体颜色),这一切其实很简单。
首先我们改变ListStore对象使它可以存放我们需要的颜色信息。
1 ListStore musicList = new ListStore(typeof(string),typeof(string),typeof(string),typeof(string));
2 musicList.AppendValues("beyond","red","海阔天空","blue");
3 musicList.AppendValues("羽泉","red","彩虹","blue");
2 musicList.AppendValues("beyond","red","海阔天空","blue");
3 musicList.AppendValues("羽泉","red","彩虹","blue");
我们的musicList共有四列,其中第2,4列存放了单元格的颜色。
然后增加一条属性映射。
1 TreeViewColumn artistCol = new TreeViewColumn();
2 artistCol.Title = "Artist";
3 CellRendererText artistNameCell = new CellRendererText();
4 artistCol.PackStart(artistNameCell,true);
5 artistCol.AddAttribute(artistNameCell,"text",0);
6 artistCol.AddAttribute(artistNameCell,"foreground",1);
2 artistCol.Title = "Artist";
3 CellRendererText artistNameCell = new CellRendererText();
4 artistCol.PackStart(artistNameCell,true);
5 artistCol.AddAttribute(artistNameCell,"text",0);
6 artistCol.AddAttribute(artistNameCell,"foreground",1);
第六行增加了一条对CellRendererText的Foreground属性(Property)的映射。
完整的代码如下
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.AddAttribute(artistNameCell,"text",0);
26 artistCol.AddAttribute(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.AddAttribute(songTitleCell,"text",2);
34 songCol.AddAttribute(songTitleCell,"foreground",3);
35
36 tree.AppendColumn(artistCol);
37 tree.AppendColumn(songCol);
38
39 ListStore musicList = new ListStore(typeof(string),typeof(string),typeof(string),typeof(string));
40 musicList.AppendValues("beyond","red","海阔天空","blue");
41 musicList.AppendValues("羽泉","red","彩虹","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 }
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.AddAttribute(artistNameCell,"text",0);
26 artistCol.AddAttribute(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.AddAttribute(songTitleCell,"text",2);
34 songCol.AddAttribute(songTitleCell,"foreground",3);
35
36 tree.AppendColumn(artistCol);
37 tree.AppendColumn(songCol);
38
39 ListStore musicList = new ListStore(typeof(string),typeof(string),typeof(string),typeof(string));
40 musicList.AppendValues("beyond","red","海阔天空","blue");
41 musicList.AppendValues("羽泉","red","彩虹","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 }
运行结果