将绑定列表数据绑定到组合框和删除项目 [英] Databinding bindinglist to combobox and removing items

查看:219
本文介绍了将绑定列表数据绑定到组合框和删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var items = new BindingList< p>我想使用窗体数据绑定将组合框挂接到ViewModel类。 ; Person>(); 
comboBox.DataSource = items;
comboBox.DisplayMember =Name;

除了从列表中删除项目,所有工作都正常。例如,如果我删除当前选择的项目(在组合框中选择),组合框的selectedIndexChanged和SelectedValueChanged事件不会触发。

解决方案

找到答案。我不得不使用BindingSource作为中间人

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

这样,当我删除某些内容时, >

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.

解决方案

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.

这篇关于将绑定列表数据绑定到组合框和删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆