我使用nodeview对象以这种方式向用户输出数据(gtk教程):

    [Gtk.TreeNode (ListOnly=true)]
    public class MyTreeNode : Gtk.TreeNode {

            string song_title;

            public MyTreeNode (string artist, string song_title)
            {
                    Artist = artist;
                    this.song_title = song_title;
            }

            [Gtk.TreeNodeValue (Column=0)]
            public string Artist;

            [Gtk.TreeNodeValue (Column=1)]
            public string SongTitle {get { return song_title; } }
    }

Gtk.NodeStore store;
    Gtk.NodeStore Store
    {
        get {
                if (store == null)
                {
                    store = new Gtk.NodeStore (typeof(MyTreeNode));
                    store.AddNode (new MyTreeNode ("The Beatles", "Yesterday"));
                    store.AddNode (new MyTreeNode ("Peter Gabriel", "In Your Eyes"));
                    store.AddNode (new MyTreeNode ("Rush", "Fly By Night"));
                }
            return store;
            }
    }

protected void OnButton1Clicked (object sender, EventArgs e)
{
    // Create a column with title Artist and bind its renderer to model column 0
    nodeview1.AppendColumn ("Artist", new Gtk.CellRendererText (), "text", 0);

    // Create a column with title 'Song Title' and bind its renderer to model column 1
    nodeview1.AppendColumn ("Song Title", new Gtk.CellRendererText (), "text", 1);
        nodeview1.ShowAll ();

    nodeview1.NodeStore=Store;

}

但我怎么能给几行nodeview(披头士乐队)涂上颜色,比如“昨天”?我试着改变nodeview的风格:背景,基色,前景和其他,但它不起作用。
编辑:我刚刚意识到,我可以这样改变栏目的颜色:
protected void OnButton1Clicked (object sender, EventArgs e)
{
    // Create a column with title Artist and bind its renderer to model column 0
    nodeview1.AppendColumn ("Artist", new Gtk.CellRendererText (), "text", 0);



    // Create a column with title 'Song Title' and bind its renderer to model column 1
    nodeview1.AppendColumn ("Song Title", new Gtk.CellRendererText (), "text", 1);
        nodeview1.ShowAll ();

    nodeview1.NodeStore=Store;
    nodeview1.Columns[0].Cells[0].CellBackgroundGdk=new Gdk.Color(0,255,0);
}

但是我怎样才能改变特定细胞的颜色呢?

最佳答案

要按行更改渲染属性,有两种可能
定义模型中的属性,然后在创建单元呈现器时引用它们;或
编写自己的TreeCellDataFunc并将其绑定到单元格渲染器,以确保在渲染之前更改其属性。
选项1速度更快,但限制您使用静态定义的值;当呈现依赖于多个变量且涉及到一些逻辑时,选项2更好。
我会教你如何做1,并参考Mono documentation中的2。
如果要更改前景色,只需向模型中添加新列:

[Gtk.TreeNode (ListOnly=true)]
public class MyTreeNode : Gtk.TreeNode
{
    public MyTreeNode (string artist, string title, string color)
    {
        Artist = artist;
        Title = title;
        Color = color;
    }

    [Gtk.TreeNodeValue (Column=0)]
    public string Artist;

    [Gtk.TreeNodeValue (Column=1)]
    public string Title;

    [Gtk.TreeNodeValue (Column=2)]
    public string Color;
}

将有效的gdk颜色表示形式(如“red”或“ff0000”)传递给行构造函数:
store.AddNode(new MyTreeNode("The Beatles", "Yesterday", "red"));
store.AddNode(new MyTreeNode("Peter Gabriel", "In Your Eyes", "black"));

然后,在构建视图时,只需将模型列(2)绑定到单元渲染器的“前台”属性:
nodeview1.AppendColumn("Artist", new Gtk.CellRendererText(),
                       "text", 0, "foreground", 2);

nodeview1.AppendColumn("Song Title", new Gtk.CellRendererText(),
                       "text", 1, "foreground", 2);

这就是全部。

08-19 06:02