1
我有一個TreeView與ListStore模型和3個文本列(使用CellRenderText)。Vala:TreeVIew + ListStore行背景顏色
我的問題是如果有什麼辦法可以改變背景色一行。當你選擇一行時,它的顏色發生了變化,我可以在不點擊它的情況下得到與某個隨機行相同的效果。
我有一個TreeView與ListStore模型和3個文本列(使用CellRenderText)。Vala:TreeVIew + ListStore行背景顏色
我的問題是如果有什麼辦法可以改變背景色一行。當你選擇一行時,它的顏色發生了變化,我可以在不點擊它的情況下得到與某個隨機行相同的效果。
簡單的方法是讓模型中的一列設置背景顏色。
下面是一個例子,您可以切換的第三排背景色:
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;
}
}
的結果應該是這樣的:
這裏觸發手冊,但你可以有業務邏輯決定哪一行更改...
它是完美的!謝謝! (這是你第二次解決我的問題,至少對我來說是無法解決的問題) – bcedu
@bcedu樂於幫助:) –