Vala: TreeVIew + ListStore 行背景色
Vala: TreeVIew + ListStore row background color
我有一个带有 ListStore 模型和 3 个文本列的 TreeView(使用 CellRenderText 制作)。
我的问题是有没有办法改变一行的背景颜色。当你 select 一行颜色改变时,我可以在不点击它的情况下用一些随机行获得相同的效果吗?
简单的方法是让模型中的一列设置背景颜色。
这是一个可以切换第三行背景颜色的示例:
public class Application : Gtk.Window {
public Application () {
// Prepare Gtk.Window:
this.title = "My Gtk.TreeView";
this.window_position = Gtk.WindowPosition.CENTER;
this.destroy.connect (Gtk.main_quit);
this.set_default_size (350, 70);
Gtk.Box box = new Gtk.Box (Gtk.Orientation.VERTICAL, 6);
// The Model:
Gtk.ListStore list_store = new Gtk.ListStore (2, typeof (string), typeof (Gdk.RGBA));
Gtk.TreeIter iter;
list_store.append (out iter);
list_store.set (iter, 0, "Stack", 1, "#FFFFFF");
list_store.append (out iter);
list_store.set (iter, 0, "Overflow", 1, "#FFFFFF");
list_store.append (out iter);
list_store.set (iter, 0, "Vala", 1, "#FFFFFF");
list_store.append (out iter);
list_store.set (iter, 0, "Gtk", 1, "#FFFFFF");
// The View:
Gtk.TreeView view = new Gtk.TreeView.with_model (list_store);
box.add (view);
Gtk.ToggleButton button = new Gtk.ToggleButton.with_label ("Change bg color row 3");
box.add (button);
this.add (box);
Gtk.CellRendererText cell = new Gtk.CellRendererText ();
view.insert_column_with_attributes (-1, "State", cell, "text", 0, "background-rgba", 1);
// Setup callback to change bg color of row 3
button.toggled.connect (() => {
// Reuse the previous TreeIter
list_store.get_iter_from_string (out iter, "2");
if (!button.get_active ()) {
list_store.set (iter, 1, "#c9c9c9");
} else {
list_store.set (iter, 1, "#ffffff");
}
});
}
public static int main (string[] args) {
Gtk.init (ref args);
Application app = new Application ();
app.show_all ();
Gtk.main ();
return 0;
}
}
结果应该是这样的:
此处触发器是手动的,但您可以让业务逻辑决定要更改哪一行...
我有一个带有 ListStore 模型和 3 个文本列的 TreeView(使用 CellRenderText 制作)。
我的问题是有没有办法改变一行的背景颜色。当你 select 一行颜色改变时,我可以在不点击它的情况下用一些随机行获得相同的效果吗?
简单的方法是让模型中的一列设置背景颜色。
这是一个可以切换第三行背景颜色的示例:
public class Application : Gtk.Window {
public Application () {
// Prepare Gtk.Window:
this.title = "My Gtk.TreeView";
this.window_position = Gtk.WindowPosition.CENTER;
this.destroy.connect (Gtk.main_quit);
this.set_default_size (350, 70);
Gtk.Box box = new Gtk.Box (Gtk.Orientation.VERTICAL, 6);
// The Model:
Gtk.ListStore list_store = new Gtk.ListStore (2, typeof (string), typeof (Gdk.RGBA));
Gtk.TreeIter iter;
list_store.append (out iter);
list_store.set (iter, 0, "Stack", 1, "#FFFFFF");
list_store.append (out iter);
list_store.set (iter, 0, "Overflow", 1, "#FFFFFF");
list_store.append (out iter);
list_store.set (iter, 0, "Vala", 1, "#FFFFFF");
list_store.append (out iter);
list_store.set (iter, 0, "Gtk", 1, "#FFFFFF");
// The View:
Gtk.TreeView view = new Gtk.TreeView.with_model (list_store);
box.add (view);
Gtk.ToggleButton button = new Gtk.ToggleButton.with_label ("Change bg color row 3");
box.add (button);
this.add (box);
Gtk.CellRendererText cell = new Gtk.CellRendererText ();
view.insert_column_with_attributes (-1, "State", cell, "text", 0, "background-rgba", 1);
// Setup callback to change bg color of row 3
button.toggled.connect (() => {
// Reuse the previous TreeIter
list_store.get_iter_from_string (out iter, "2");
if (!button.get_active ()) {
list_store.set (iter, 1, "#c9c9c9");
} else {
list_store.set (iter, 1, "#ffffff");
}
});
}
public static int main (string[] args) {
Gtk.init (ref args);
Application app = new Application ();
app.show_all ();
Gtk.main ();
return 0;
}
}
结果应该是这样的:
此处触发器是手动的,但您可以让业务逻辑决定要更改哪一行...