ListBox databinding question

226 views
Skip to first unread message

Yves Schelpe

unread,
Apr 10, 2015, 5:55:11 AM4/10/15
to eto-...@googlegroups.com
Hi,

I currently have the following code:
_dependenciesListBox.DataContext = ViewModel.Dependencies;  

ViewModel.Dependencies is an IEnumerable<string>. The databinding is not kicking in using Eto.Forms 2.0.7.
The enumerable has values in it at runtime. What should I do to get the databinding in eto.forms going?

Thanks,
Yves

curtis

unread,
Apr 10, 2015, 12:03:48 PM4/10/15
to eto-...@googlegroups.com
The DataContext is used for mvvm binding to your data model.  To bind to your IEnumerable<string>, use the ListBox.DataStore property.

If you want to use MVVM to its fullest, where if you switch out your view model it'll change the list, you can use both the DataContext and DataStore like so:

public class TestForm : Form
{
     
class MyModel
     
{
         
public List<string> ListItems { get; set; }
     
}
 
     
public TestForm()
     
{
         
ClientSize = new Size(300, 300);
 
         
var listBox = new ListBox();
         listBox
.BindDataContext(c => c.DataStore, (MyModel m) => m.ListItems);
 
         
// button to change the data context
         
var changeDataContext = new Button { Text = "Change Data Context" };
         changeDataContext
.Click += (sender, e) => DataContext = new MyModel { ListItems = new List<string> { "Some", "Other", "Items" } };
 
         
Content = new TableLayout
         
{
             
Rows =
             
{
                 changeDataContext
,
                 listBox
             
}
         
};
 
         
// set initial data context for all controls of the form
         
DataContext = new MyModel { ListItems = new List<string> { "Hello", "There" } };
     
}
}


Hope this helps!

Curtis.
Reply all
Reply to author
Forward
0 new messages