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.