简体   繁体   中英

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

I am trying my hands on WPF. I am trying to bind a ComboBox with List<MyClass> where MyClass is user defined class. I want to set SelectedValuePath property of ComboBox so that, value of ComboBox item represents object of MyClass .

I tried setting SelectedValuePath property to . and this , but no luck. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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