WPF学习笔记:ComboBox的数据绑定
UI
VM
static Dictionary olist = new Dictionary { {1,"有钱" } ,{2,"有闲" } ,{3,"有料" } ,{4,"有鬼" } }; public Dictionary StatusList { get { return olist; } } KeyValuePair _kvp; public KeyValuePair SelectedStatus { get { return this._kvp; } set { this._kvp = value; this.RaisePropertyChanged("SelectedStatus"); } } DelegateCommand _SelectionChangedCmd = null; public DelegateCommand SelectionChangedCmd { get { if (this._SelectionChangedCmd == null) { this._SelectionChangedCmd = new DelegateCommand(SelectionChanged); } return this._SelectionChangedCmd; } } void SelectionChanged(ComboBox cb) { SelectedStatus = (KeyValuePair)cb.SelectedItem; Status = SelectedStatus.Key; } public User.EStatus Status { set { var query = olist.Where(n => n.Key == value); if (query != null && query.Count() > 0) { this.SelectedStatus = query.First(); } } }
注意里面用了Dictionary<>作为数据源的类型,因此有ComboBox控件里有
SelectedValuePath="Key" DisplayMemberPath="Value"
参考文章:
http://luacloud.com/2011/wpf-combobox-binding-data.html
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~