dictionary - 如何在列表框中显示字典

标签 dictionary listbox

我正在尝试将键/值对从字典显示到列表框。

Key Value
A    10
B    20
C    30

我想以下列格式在列表框中显示它们

A(10)
B(20)
C(30)

使用以下代码,我已经能够将 Listbox.Datasource 链接到 Dictionary。

myListBox.DataSource = new BindingSource(myDictionary, null);

它显示为

[A, 10]
[B, 20]
[C, 30]

我不知道如何格式化它以便它以我想要的方式显示。

我们将不胜感激。

谢谢 阿希什

最佳答案

在列表框上使用 Format 事件:

KeyValuePair<string, int> item = (KeyValuePair<string, int>)e.ListItem;
e.Value = string.Format("{0}({1})", item.Key, item.Value);

关于dictionary - 如何在列表框中显示字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/885271/

相关文章:

c# - 如何在绑定(bind) ItemsSource 时将按钮添加到 wpf 列表框

windows-phone-7 - 在 Windows Phone 7 的列表框中捕获 "Click"的正确事件是什么?

c++ - 使用列表c++在 map 中查找元素

python - 在字典中的另一个列表中查找列表的最大长度

python获取列表中元组的第二个值

c# - 在 C# WinForms 中为 Listbox/ListView 行的部分着色的方法?

将列表列表拆分为长度键控字典的 pythonic 方法?

python - Python中dict对象的联合

c# - 隐藏ListBox控件中的垂直滚动条

c# - 将数据集绑定(bind)到仅显示 1 条记录的列表框