在vb.net /的WinForms,怎能一个Hashtable绑定到一个下拉列表或任何其他数据源驱动的控制?
In vb.net / winforms, how can a hashtable be bound to a drop down list or any other datasource-driven control?
这是的WinForms,WPF,或asp.net? [更新:啊...的WinForms ;-p]
Is this winforms, wpf, or asp.net? [update: ahh... winforms ;-p]
的WinForms想要的数据是的IList
(或间接地通过 IListSource
) - 所以我猜(从评论)所使用的WinForms。没有一个内置字典的集合实施的IList
,但说实话这并不重要:如果你是数据绑定,体积大概是相当小的,所以常规列表应该就可以了。
winforms wants data to be IList
(or, indirectly, via IListSource
) - so I'm guessing (from the comment) that you are using winforms. None of the inbuilt dictionary-like collections implement IList
, but to be honest it doesn't matter: if you are data-binding, the volume is probably fairly small, so a regular list should be fine.
最好的办法是像一个名单,其中,T>
或的BindingList< T>
,其中 T
拥有所有你要绑定的属性。这是一个选择吗?如果你被卡住1.1(因为你提到的HashTable
,而不是词典<,>
),然后使用的ArrayList
。
The best option is something like a List<T>
or BindingList<T>
, where T
has all the properties you want to bind to. Is this an option? If you are stuck with 1.1 (since you mention HashTable
rather than Dictionary<,>
), then use ArrayList
.
例(在C#):
class MyData
{
public int Key { get; set; }
public string Text { get; set; }
}
[STAThread]
static void Main()
{
var data = new List<MyData>
{
new MyData { Key = 1, Text = "abc"},
new MyData { Key = 2, Text = "def"},
new MyData { Key = 3, Text = "ghi"},
};
ComboBox cbo = new ComboBox
{
DataSource = data,
DisplayMember = "Text",
ValueMember = "Key"
};
cbo.SelectedValueChanged += delegate {
Debug.WriteLine(cbo.SelectedValue);
};
Application.Run(new Form {Controls = {cbo}});
}