yuuki yuuki - 1 year ago 81
C# Question

syncing a GTK iterTree with a list

i have the following problem,
i created a tree model with the folowing arguments :

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;


the in the code used mods consists of an internal definition:
internal List Mods = new List();

Fore refrence here the used 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; }


the function i currently use looks like:

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


}


but this one doesnt accepts the list entries. So is there a way for it to also accept the in memory list.

Answer Source

The model creation seems wrong. You probably don't won't to have a List as the type of the model items. Probably you want the type the list contains:

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

Assuming that this class SDVMMR.ModInfo is the class with the properties you have indicated, then to add the items in your list and populate the model, you should do something like:

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

You also need to customize the cell data functions in order for the treeview to know where to get the data from.

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

These methods must be setup on the cell renderers:

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

Your code is incomplete and missing a few hints so the answer may also be incomplete.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download