c# - 如何在 WinForms 中将字典绑定(bind)到列表框

标签 c# winforms dictionary data-binding listbox

可以将字典绑定(bind)到列表框,使列表框和成员属性保持同步吗?

最佳答案

var choices = new Dictionary<string, string>(); 
choices["A"] = "Arthur"; 
choices["F"] = "Ford"; 
choices["T"] = "Trillian"; 
choices["Z"] = "Zaphod"; 
listBox1.DataSource = new BindingSource(choices, null); 
listBox1.DisplayMember = "Value"; 
listBox1.ValueMember = "Key"; 

(无耻地摘 self 自己的博客:Bind a ComboBox to a generic Dictionary。)

这意味着您可以使用 SelectedValue 获取 ListBox 中所选项目的相应字典键。

关于c# - 如何在 WinForms 中将字典绑定(bind)到列表框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1506987/

相关文章:

c# - 我可以使用 Base64String 中的 body.insertFileFromBase64 来自 doc 文件而不是 docx

c# - 使用 rundll32.exe 启动一次单击部署 URL?

c# - 自定义授权(权限)ASP.NET MVC

c# - C# 是否有 Java JNA 库的替代方案?

dictionary - 转换 Maps Go 语言中 VALUES 的数据类型

C#如何将不规则的日期时间String转换成DateTime?

c# - Linq 查询中的意外结果

c# - 防止 WebBrowser 在导航时导致 UI 卡住?

python - 将带有键和值的字符串转换为 python 字典的最有效方法

swift - 如何在 swift 中对不同字典中同一键的多个值求和?