简体   繁体   English

将ComboBox与Generic List绑定,以便组合框项目的值表示绑定对象

[英]Binding ComboBox with Generic List so that value of combobox item represents bound object

I am trying my hands on WPF. 我正在尝试使用WPF。 I am trying to bind a ComboBox with List<MyClass> where MyClass is user defined class. 我试图用List<MyClass>绑定ComboBox ,其中MyClass是用户定义的类。 I want to set SelectedValuePath property of ComboBox so that, value of ComboBox item represents object of MyClass . 我想设置ComboBox的SelectedValuePath属性,以便ComboBox项的值表示MyClass对象。

I tried setting SelectedValuePath property to . 我尝试将SelectedValuePath属性设置为. and this , but no luck. this ,但是没有运气。 Can anybody suggest me the way to achieve it? 有人可以建议我实现这一目标的方法吗?

Example Code : 示例代码:

Class MyClass
{
   public int ID {get; set;}
   public string Name {get; set;}
}

List<MyClass> lst = new List<MyClass>();

ComboBox cmb = new ComboBox();
cmb.DataContext = lst;
cmb.DisplayMemberPath = "Name";
// Here I want object of MyClass (which is bound to this item) itself should be assigned as value.
cmb.SelectedValuePath = "????"; 

使用cmb.SelectedItem代替它返回一个对象,将其cmb.SelectedItemMyClass并且完全不设置SelectedValuePath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM