Skip to content

Instantly share code, notes, and snippets.

@cwensley
Last active December 8, 2015 09:32
Show Gist options
  • Save cwensley/1540f0aa41ea92adb2e0 to your computer and use it in GitHub Desktop.
Save cwensley/1540f0aa41ea92adb2e0 to your computer and use it in GitHub Desktop.
Bind Eto ComboBox to a Dictionary
using Eto.Forms;
using System.Linq;
using System.Collections.Generic;
class MyModel
{
public Guid MyGuidProperty {get; set; }
}
public class BindToDictionaryTest : StackLayout
{
public BindToDictionaryTest()
{
var control = new ComboBox();
// text is the value of the dictionary
control.ItemTextBinding = Binding.Property((KeyValuePair<Guid, string> r) => r.Value);
// key is the guid (as a string)
control.ItemKeyBinding = Binding.Property((KeyValuePair<Guid, string> r) => r.Key).Convert(r => r.ToString());
// create dictionary and set it as the ComboBox's data store
var dic = new Dictionary<Guid, string>();
dic.Add(Guid.NewGuid(), "Item 1");
dic.Add(Guid.NewGuid(), "Item 2");
dic.Add(Guid.NewGuid(), "Item 3");
control.DataStore = dic.Cast<object>(); // cast to object as KeyValuePair is a struct
Items.Add(control);
// bind to a Guid property of the DataContext by converting to/from string:
control.SelectedKeyBinding
.Convert(r => new Guid(r), g => g.ToString())
.BindDataContext((MyModel m) => m.MyGuidProperty);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment