【发布时间】:2011-02-02 14:39:46
【问题描述】:
我正在尝试将字典作为 DataSource 绑定到 ListBox。 How to bind a dicationary to a ListBox in winforms 中的解决方案对我不起作用,因为我的字典是类级变量而不是方法级变量,所以我不能使用 var。当您将一个类级变量放入 new BindingSource(...) 时,将 null 作为第二个参数,我得到一个 ArgumentNull 异常。
如何将类级字典绑定为列表框的数据源?
我不喜欢 List> 解决方法,因为与 TryGetValue(...) 相比, Where(...) 和 First(...) 丑陋、复杂且令人困惑和其他字典功能。
namespace myNamespace
{
public partial class myForm : Form
{
private Dictionary<string,string> myDictionay;
public myForm()
{
InitializeComponent();
myDictionay= new Dictionary<string, string>();
listBox1.DataSource = new BindingSource(myDictionay,null); // ArguemtNull exception
}
}
}
【问题讨论】:
-
你确定你对象上的字典不为空吗?我们可能需要更多代码
-
我在 DataSource 分配上方的一行实例化字典 - 字典不为空。
标签: c# dictionary listbox datasource