public static void Bind <TBinding, TSource, TProperty>( this ComboBox comboBox, TBinding item, Expression <Func <TBinding, TProperty> > member, ComboBoxSource.ComboBoxBinding <TSource> binding) { BindSource(comboBox, binding); BindSelectedItem(comboBox, item, member, binding); }
public Bind <TBinding> For <TSource, TProperty>( ComboBox comboBox, Expression <Func <TBinding, TProperty> > member, ComboBoxSource.ComboBoxBinding <TSource> binding) { comboBox.Bind(_item, member, binding); return(this); }
private static void BindSelectedItem <TBinding, TSource, TProperty>(ComboBox comboBox, TBinding item, Expression <Func <TBinding, TProperty> > member, ComboBoxSource.ComboBoxBinding <TSource> binding) { comboBox.DataBindings.Add(nameof(ComboBox.SelectedItem), item, ReflectionUtils.GetPropertyName(member)); comboBox.SetDefault(binding.DefaultValue); comboBox.SelectionChangeCommitted += (sender, e) => comboBox.DataBindings[nameof(ComboBox.SelectedItem)].WriteValue(); (comboBox.DataSource as IBindingList).ListChanged += (sender, e) => { var value = ReflectionUtils.GetValue(item, member); if (value == null) { comboBox.SetDefault(binding.DefaultValue); } }; }
private static void BindSource <TSource>(ComboBox comboBox, ComboBoxSource.ComboBoxBinding <TSource> binding) { comboBox.DataSource = binding.Source; comboBox.DisplayMember = binding.DisplayMember; comboBox.ValueMember = binding.ValueMember; }