将GTK iterTree与列表同步

时间:2017-07-25 22:18:33

标签: c# list mono treeview gtk

我有以下问题, 我用下面的参数创建了一个树模型:

    this.Mods = SDVMMR.ModListManagment.LoadList(ModStore);

    // Createing  columns
    Gtk.TreeViewColumn CBColumn = new Gtk.TreeViewColumn();
    CBColumn.Title = "Active";

    Gtk.CellRendererText CBCell = new Gtk.CellRendererText();
    CBColumn.PackStart(CBCell, true);

    Gtk.TreeViewColumn NameColumn = new Gtk.TreeViewColumn();
    NameColumn.Title = "Name";

    Gtk.CellRendererText ModsNameCell = new Gtk.CellRendererText();
    NameColumn.PackStart(ModsNameCell, true);

    Gtk.TreeViewColumn AuthorColumn = new Gtk.TreeViewColumn();
    AuthorColumn.Title = "Author";

    Gtk.CellRendererText AuthorCell = new Gtk.CellRendererText();
    AuthorColumn.PackStart(AuthorCell, true);

    Gtk.TreeViewColumn VersionColumn = new Gtk.TreeViewColumn();
    VersionColumn.Title = "Version";

    Gtk.CellRendererText VersionCell = new Gtk.CellRendererText();
    VersionColumn.PackStart(VersionCell, true);

    // Add the columns to the TreeView
    activeMods.AppendColumn(CBColumn);
    activeMods.AppendColumn(NameColumn);
    activeMods.AppendColumn(AuthorColumn);
    activeMods.AppendColumn(VersionColumn);

    NameColumn.AddAttribute(ModsNameCell, "text", 1);
    AuthorColumn.AddAttribute(AuthorCell, "text", 2);
    VersionColumn.AddAttribute(VersionCell, "text",3);

    // the column checkbox is created
    Gtk.CellRendererToggle valueCb = new CellRendererToggle();
    CBColumn.PackStart(valueCb, true);




    activeMods.Model = ModStore;

在使用的代码中,mods由一个内部定义组成: 内部列表Mods = new List();

此处使用的是ModInfo:

    public string Name { get; set; }
    public string Author { get; set; }
    public string Version { get; set; }
    public string UniqueID { get; set; }
    public string MinimumApiVersion { get; set; }
    public string Description { get; set; }
    public string EntryDll { get; set;}
    public bool IsActive { get; set; }
    public bool IsXnb { get; set; }
    public string OrgXnbPath { get; set; }

我目前使用的功能如下:

        internal static void addToTree(ModInfo Mod, ListStore ModStore)
    {

        /*Gtk.TreeIter iter = ModStore.AppendValues(Mod.Name);
        ModStore.AppendValues(iter, "Author", Mod.Author);
        ModStore.AppendValues(iter, "Version", Mod.Version);
        ModStore.AppendValues(iter, "Description", Mod.Description);
        ModStore.AppendValues(iter, "Is Active", Mod.IsActive.ToString());*/

    ModStore.AppendValues(Mod.IsActive.ToString(),Mod.Name,Mod.Author,Mod.Version);


    }

但是这个不接受列表条目。那么有没有办法让它也接受内存列表。

1 个答案:

答案 0 :(得分:0)

模型创建似乎错了。您可能不会将List作为模型项的类型。可能你想要列表包含的类型:

... new Gtk.TreeStore(typeof(SDVMMR.ModInfo));

假设此类SDVMMR.ModInfo是具有您指定的属性的类,那么要在列表中添加项并填充模型,您应该执行以下操作:

foreach (ModInfo Mod in List<SDVMMR.ModInfo>) {
   activeMods.AppendValues (Mod);
}

您还需要自定义单元格数据函数,以便树视图知道从何处获取数据。

private void RenderNameCell (Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
{
    SVDMMR.ModInfo modinfo = (SVDMMR.ModInfo) model.GetValue (iter, 0);
    (cell as Gtk.CellRendererText).Text = modinfo.name;
}

private void RenderValueCell (Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
{
    // You didn't say what the value is so lets use UniqueID

    SVDMMR.ModInfo modinfo = (SVDMMR.ModInfo) model.GetValue (iter, 0);
    (cell as Gtk.CellRendererText).Text = modinfo.UniqueID;
}

必须在单元格渲染器上设置这些方法:

NameColumn.SetCellDataFunc (ModsNameCell, new Gtk.TreeCellDataFunc (RenderNameCell));
ValueColumn.SetCellDataFunc (ModValueCell, new Gtk.TreeCellDataFunc (RenderValueCell));

您的代码不完整,缺少一些提示,因此答案也可能不完整。