2017-07-26 29 views
1

私はListStoreモデルと3つのテキスト列(CellRenderTextで作られた)を持つTreeViewを持っています。Vala:TreeVIew + ListStore行の背景色

私の質問は、のの背景色を変更する方法があるかどうかです。色を変更した行を選択すると、クリックしないでいくつかのランダムな行で同じ効果を得ることができますか?

答えて

1

単純なアプローチは、モデルの列に背景色を設定することです。ここで

あなたは三行の背景色を切り替えることができます例です。

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; 
    } 
} 

結果はこのようなものでなければなりません:トリガーは手動ですが、することができます。ここ

enter image description here

ビジネスロジックがどの行を変更するかを決定してください...

+0

これは完璧です!ありがとう! (少なくとも私の解決できない問題のために、あなたが私のうちの1つを解決するのは2回目です) – bcedu

+0

@bcedu助けて嬉しいです:) –