如何绑定到词典列表框的WinForms中 [英] How to bind Dictionary to ListBox in winforms

查看:128
本文介绍了如何绑定到词典列表框的WinForms中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是可能的词典绑定到一个列表框,在保持同步列表框和成员属性之间?


解决方案

  VAR的选择=新词典<字符串,字符串>();
选择[A] =亚瑟
选择[F] =福特;
选择[T] =Trillian的;
选择[Z] =赞福德;
listBox1.DataSource =新的BindingSource(选择,NULL);
listBox1.DisplayMember =值;
listBox1.ValueMember =键;

(无耻地从我自己的博客解禁:绑定一个ComboBox通用字典。)

这意味着你可以使用的SelectedValue来获得相应的字典键搁置ListBox中选定的项目。

It is possible to bind a dictionary to a listbox, keeping in sync between the listbox and the member property?

解决方案

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";

(Shamelessly lifted from my own blog: Bind a ComboBox to a generic Dictionary.)

This means you can use SelectedValue to get hold of the corresponding dictionary key for the selected item in the ListBox.

这篇关于如何绑定到词典列表框的WinForms中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆