【发布时间】:2014-08-03 09:40:24
【问题描述】:
我正在尝试创建具有键值对的 ListBox。我从类中获得的那些数据是从 getter 中提供的。
类:
public class myClass
{
private int key;
private string value;
public myClass() { }
public int GetKey()
{
return this.key;
}
public int GetValue()
{
return this.value;
}
}
程序:
private List<myClass> myList;
public void Something()
{
myList = new myList<myClass>();
// code for fill myList
this.myListBox.DataSource = myList;
this.myListBox.DisplayMember = ??; // wanted something like myList.Items.GetValue()
this.myListBox.ValueMember = ??; // wanted something like myList.Items.GetKey()
this.myListBox.DataBind();
}
与本主题 [Cannot do key-value in listbox in C#] 类似,但我需要使用从方法返回值的类。
是否可以做一些简单的事情,或者我最好完全重新设计我的思路(和这个解决方案)?
感谢您的建议!
【问题讨论】:
-
myList是list<myclass>。因此,即使您可以说myList.Items.GetValue(),那么对于哪个特定的myclass对象,它应该得到key/value?所以,这根本不合逻辑。 -
谢谢。我是初学者,不知道如何创建自己的属性以及它有什么好处。现在它解决了我的问题!
标签: c# function listbox valuemember