[Gtk-sharp-list] [Mono GTK#] Bind Object List to ComboBox

Ганьков Андрей gankov.andrey at inbox.ru
Tue Sep 17 17:04:48 UTC 2013


17.09.2013 19:40, jaymarvels пишет:
> Coming from a winform background I could do this without an issue..in gtk# I
> am running into all kinds of issues.
>
> I have something like this:
>
> 			var dataSource = new List<Positions>();
> 			dataSource.Add(new Positions() { positionName = "Goalkeeper",
> positionMask = 1 });
> 			dataSource.Add(new Positions() { positionName = "Sweeper", positionMask =
> 2 });
> 			dataSource.Add(new Positions() { positionName = "Defender", positionMask
> = 4 });
> 			dataSource.Add(new Positions() { positionName = "Defensive Mid",
> positionMask = 8 });
> 			dataSource.Add(new Positions() { positionName = "Midfielder",
> positionMask = 16 });
> 			dataSource.Add(new Positions() { positionName = "Attacking Mid",
> positionMask = 32 });
> 			dataSource.Add(new Positions() { positionName = "Striker", positionMask =
> 64 });
> 			//Setup data binding for drop down
> this.cboPlayerPosition.datasource = dataSource;
> 			this.cboPlayerPosition.DisplayMember = "positionName";
> 			this.cboPlayerPosition.ValueMember = "positionMask";
>
> This failed as GTK# doesnt have .datasource ...
>
> How can I accomplish what I am trying..
>
> I have googled for hours trying to see how to bind a object list to a
> combobox with no luck.
You need use Gtk.ListStore.

If i understand your task, you can do it like this:

Gtk.ListStore dataSource = new ListStore (typeof (string), typeof (int));
dataSource.AppendValues( "Goalkeeper", 1);
dataSource.AppendValues ("Sweeper", 2);
dataSource.AppendValues ( "Defender", 4 );
dataSource.AppendValues("Defensive Mid", 8);
.......
//If you want display only name column, you need setup combobox
combo.Clear ();
Gtk.CellRendererText text = new Gtk.CellRendererText ();
combo.Model = dataSource;
combo.PackStart (text, false);
combo.AddAttribute (text, "text", 0);

//You can get active item value like this:
TreeIter iter;
if(combo.GetActiveIter(out iter))
     positionMask = (int) combo.Model.GetValue(iter,1);

I think if you want use "Positions" class for store data, also you can 
make it by using "SetCellDataFunc" . See example here
http://www.mono-project.com/GtkSharp_TreeView_Tutorial

-- 
Andrey Gankov
Mail:gankov.andrey at inbox.ru
ICQ: 230-684-976
Jabber:gankov at jabber.ru

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/gtk-sharp-list/attachments/20130917/81b1a9c2/attachment.html>


More information about the Gtk-sharp-list mailing list