[英]Is it possible to set listbox datasource to List<Tuple<string, string, int>> Item1 only?
I have 我有
public static class GlobalVariables
{
public static List<Tuple<string, string, int>> PopFile;
}
and I'm trying to use PopFile as a datasource to listbox 我正在尝试使用PopFile作为列表框的数据源
listBox1.DataSource = GlobalVariables.PopFile;
The problem is that it obviously adds ([string], [string], [int]) to the listbox but I want to add only the first items of tuples. 问题是,它显然将([string],[string],[int])添加到了列表框,但是我只想添加元组的第一项。 Is that possible?
那可能吗?
I could use 我可以用
foreach (Tuple<string, string, int> i in GlobalVariables.PopFile)
{
listBox1.Items.Add(i.Item1);
}
but I'd prefer .DataSource. 但我更喜欢.DataSource。
listBox1.DataSource = GlobalVariables.PopFile;
listBox1.DisplayMember = "Item1";
listBox1.ValueMember = "Item3"; // optional
https://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.datasource https://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.datasource
In the end, you can't really avoid looping through and either individually adding your data from your tuple object or creating a new object to bind to the listbox. 最后,您真的无法避免循环遍历,要么从元组对象中单独添加数据,要么创建一个新对象以绑定到列表框。
Listbox simply doesn't have the input parameters to take in a tuple object unless you extend the Listbox object yourself to be able to handle it appropriately, but even then you will still have to do something similar to your loop above. 除非您自己扩展Listbox对象以能够正确处理它,否则Listbox根本没有输入参数来接收元组对象,但是即使那样,您仍然必须做与上述循环类似的事情。
LINQ可能会派上用场...
listBox1.DataSource = GlobalVariables.PopFile.Select(t => t.Item1).ToList()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.