gpt4 book ai didi

c# - Gtk# NodeView 彩色行

转载 作者:太空狗 更新时间:2023-10-29 23:40:03 25 4
gpt4 key购买 nike

我使用 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 的某些行着色(例如,“The Beatles” - “Yesterday”)?我尝试通过更改 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);
}

但是我怎样才能改变特定单元格的颜色呢?

最佳答案

要在每行的基础上更改渲染属性,您有两种可能性

  1. 在模型中定义属性,然后在创建单元格渲染器时引用它们;或
  2. 编写您自己的 TreeCellDataFunc 并将其绑定(bind)到单元格渲染器,以确保在渲染之前更改其属性。

选项 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"));

然后当您构建 View 时,您只需将该模型列 (2) 绑定(bind)到单元格渲染器的“前景”属性:

nodeview1.AppendColumn("Artist", new Gtk.CellRendererText(), 
"text", 0, "foreground", 2);

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

就这些。

关于c# - Gtk# NodeView 彩色行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14148679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com