[Gtk-sharp-list] [Mono GTK#] Bind Object List to ComboBox
Jay Spencer
jacob.spencer80 at googlemail.com
Tue Sep 17 17:29:02 UTC 2013
That should work quite well...especially as the list is short...
Further in my project I deserialize a xml file into a list object class and
will need to populate a combobox as well...again no problem with winforms...
Could I do a foreach loop and populate the list store like that...
So from the class into the listview via a foreach loop ?
On 17 Sep 2013 18:12, "Ганьков Андрей" <gankov.andrey at inbox.ru> wrote:
> 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 GankovMail:gankov.andrey at inbox.ru
> ICQ: 230-684-976Jabber:gankov at jabber.ru
>
>
> _______________________________________________
> Gtk-sharp-list maillist - Gtk-sharp-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/gtk-sharp-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/gtk-sharp-list/attachments/20130917/86032689/attachment.html>
More information about the Gtk-sharp-list
mailing list