简体   繁体   English

是否可以将列表框数据源设置为列表 <Tuple<string, string, int> &gt;仅限于项目1?

[英]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.

相关问题 如何在词典中查找键 <int,List<Tuple<string,string> &gt;&gt;,以使列表包含具有给定Item1和Items的元素 - How to find keys in a Dict<int,List<Tuple<string,string>>> such that the list contains elements with given Item1 and Items 从指定了item2的元组列表中选择item1 - Select item1 from tuple list where item2 is specified 如何转换清单 <Tuple<string, int> &gt;转换为字符串[]? - How to convert List<Tuple<string, int>> to string[]? 如何使用C#中的字符串从设置了数据源的列表框中删除项目 - How to remove an item from listbox with datasource set, using string in C# 字典<string, List<Tuple<string, int> &gt;&gt; dict_Info_A = 新字典<string,List<Tuple<string,int> &gt;&gt;(); - Dictionary<string, List<Tuple<string, int>>> dict_Info_A = new Dictionary<string,List<Tuple<string,int>>>(); 如何从List <Tuple <int,int,string >>获取字符串数组? - How to get array of string from List<Tuple<int, int, string>>? 使用Linq检查Tuple列表是否包含Item1 = x的元组 - Checking if list of Tuple contains a tuple where Item1 = x using Linq ListBox数据绑定与包含字符串string int的List的列表 - ListBox data binding with List of List containing string string int C#:过滤具有列表的列表框<string>作为数据源</string> - C#: Filtering listbox having List<String> as datasource 从字典 <int,List<Tuple<string,object> &gt;&gt;到字典 <int,List<Tuple<string,object,AnEnum> &gt;&gt;与LINQ - From Dictionary<int,List<Tuple<string,object>>> to Dictionary<int,List<Tuple<string,object,AnEnum>>> with LINQ
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM