GTK TreeView 复选框留给文本?

GTK TreeView checkbox left to the text?

我正在将 GTK# 与 .NET Core 结合使用。如何在文本而不是单独的列中添加一个复选框? Ubuntu的更新对话框用到了,所以一定可以,但我不知道怎么用。

我尝试将渲染器类型更改为 CellRenderToggle(),但它只显示了复选框。我认为列的类型应该是同时包含布尔值和字符串的东西,但那是什么?

var st = new Gtk.TreeStore(typeof(string), typeof(string), typeof(string));
var iter = st.AppendValues("hello", "world", "gtk");

st.AppendValues(iter, "damn", "you", "ebay");
st.AppendValues(iter, "damn", "you", "ebay");
st.AppendValues(iter, "damn", "you", "ebay");

mytree.Model = st;
mytree.ExpandAll();

var r1 = new Gtk.CellRendererToggle();
var r2 = new Gtk.CellRendererText();

mytree.Columns[0].PackStart(r1, true);
mytree.Columns[1].PackStart(r2, true);
mytree.Columns[2].PackStart(r2, true);

mytree.Columns[0].AddAttribute(r1, "text", 0);
mytree.Columns[1].AddAttribute(r2, "text", 1);
mytree.Columns[2].AddAttribute(r2, "text", 2);

您必须将两个渲染器添加到同一 Treeview 列,以反映商店中的两个不同列值。由于我不懂 C#,这里是 Python:

中的一个 MCVE
#!/usr/bin/env python

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

window = Gtk.Window()
window.set_default_size(500, 500)
tree = Gtk.TreeView()
window.add(tree)
store = Gtk.TreeStore(bool, str, str)
tree.set_model(store)
iter = store.append(None,[True, "world", "gtk"])
store.append(iter, [False, "you", "ebay"])
# create a column
column = Gtk.TreeViewColumn()
tree.append_column(column)
# add a toggle render
toggle = Gtk.CellRendererToggle()
column.pack_start(toggle, True)
column.add_attribute(toggle, "active", 0)
# and add a text renderer to the same column
text_ren = Gtk.CellRendererText()
column.pack_start(text_ren, True)
column.add_attribute(text_ren, "text", 1)
# now add a column using the default method
column = Gtk.TreeViewColumn("column", text_ren, text = 2)
tree.append_column(column)
window.show_all()

Gtk.main()

您的代码可能类似于:

mytree.Columns[0].PackStart(r1, true);
mytree.Columns[0].PackStart(r2, true);
mytree.Columns[2].PackStart(r2, true);

mytree.Columns[0].AddAttribute(r1, "text", 0);
mytree.Columns[0].AddAttribute(r2, "text", 1);
mytree.Columns[2].AddAttribute(r2, "text", 2);

有点迟了,但这是 C# 的等价物:

public class MainWindow: Gtk.Window {
    public MainWindow(): base(Gtk.WindowType.Toplevel) 
    {
        this.Build();

        this.AppendValues( true, "world", "gtk" );
        this.AppendValues( false, "you", "ebay" );
    }

    void Build()
    {
        this.BuildTreeview();

        this.DeleteEvent += (o, args) => this.Close();
        this.ShowAll();
    }

    void Close()
    {
        Gtk.Application.Quit();
    }

    void BuildTreeview()
    {
        this.TreeView = new Gtk.TreeView();
        var store = new Gtk.TreeStore( typeof( bool ), typeof( string ), typeof( string ) );

        this.TreeView.Model = store;

        // Create a column
        var column1 = new Gtk.TreeViewColumn();

        // Add a toggle render
        var toggleRenderer = new Gtk.CellRendererToggle();
        column1.PackStart( toggleRenderer, true );
        column1.AddAttribute( toggleRenderer, "active", 0 );
        column1.Title = "Column 1";

        // And add a text renderer to the same column
        var textRenderer1 = new Gtk.CellRendererText();
        column1.PackStart( textRenderer1, true );
        column1.AddAttribute( textRenderer1, "text", 1 );

        // Now add a plain text column
        var column2 = new Gtk.TreeViewColumn();
        var textRenderer2 = new Gtk.CellRendererText();
        column2.PackStart( textRenderer2, true );
        column2.AddAttribute( textRenderer2, "text", 2 );
        column2.Title = "Column 2";

        this.TreeView.AppendColumn( column1 );
        this.TreeView.AppendColumn( column2 );
        this.Add( this.TreeView );
    }

    public void AppendValues(params object[] values)
    {
        var store = (Gtk.TreeStore) this.TreeView.Model;

        store.AppendValues( values );
    }

    public Gtk.TreeView TreeView {
        get; private set;
    }
}

希望对您有所帮助。