[Mono学习]树形视图TreeView学习笔记(三):对CellRenderer的属性进行定制

我们在上一节提到可以把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 " );

 我们的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 );

 第六行增加了一条对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      }

52 }


运行结果

 

转载于:https://www.cnblogs.com/shpherd/archive/2008/10/22/1317114.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值