Databinding bindinglist to combobox and removing items

六眼飞鱼酱① 提交于 2019-12-13 16:06:22

问题


I am trying to use windows forms databinding to hook up a combobox to a ViewModel class.

var items = new BindingList<Person>();
comboBox.DataSource = items;
comboBox.DisplayMember = "Name";

All works ok except when I remove items from the list. For example if I remove the currently selected item (selected in the combobox) the selectedIndexChanged and SelectedValueChanged events of the combobox don't fire.


回答1:


Found an answer. I had to use a BindingSource as middleman

  var bindingsSource = new BindingSource();
  bindingsSource.DataSource = new BindingList<Person>();
  comboBox1.DataSource = bindingsSource;
  comboBox1.DisplayMember = "Name";

This way I do get value changed events and even more than one when I deleted something.



来源:https://stackoverflow.com/questions/7420273/databinding-bindinglist-to-combobox-and-removing-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!