可以将字典绑定(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/