问题描述
我有一个 System.Windows.Forms.Listbox
和我创建的一组元组类型值.也就是说,
我怎样才能做到这一点?
Ivan 的回答,绝对描述了案例.作为解决方法,您可以使用 ListBox
的Format
事件以显示 name
提交:
private void listBox1_Format(object sender, ListControlConvertEventArgs e){e.Value = (((string name, int ID))e.ListItem).name;}
I have a System.Windows.Forms.Listbox
and a collection of tuple type values I've created. That is, the new tuple type introduced in C# 7.0. I'm trying to bind the collection to the Listbox
and set the DisplayMember
to one of the elements in the tuple. Here's an example:
var l = new List<(string name, int ID)>()
{
("Bob", 1),
("Mary", 2),
("Beth", 3)
};
listBox1.DataSource = l;
listBox1.DisplayMember = "name";
That doesn't work, though. With the older-style Tuple<T>
you could supposedly do what's described in this answer:
listBox1.DisplayMember = "Item1";
listBox1.ValueMember = "Item3"; // optional
That doesn't work either. Here's what I'm seeing in both cases:
How can I accomplish this?
Ivan's answer, definitely describes the case. As a workaround you can use Format
event of ListBox
to show name
filed:
private void listBox1_Format(object sender, ListControlConvertEventArgs e)
{
e.Value = (((string name, int ID))e.ListItem).name;
}
这篇关于如何将 C# 7.0 元组类型值的集合绑定到 System.Windows.Forms.Listbox 并将显示成员设置为元素之一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!