繁体   English   中英

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

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

我正在尝试使用WPF。 我试图用List<MyClass>绑定ComboBox ,其中MyClass是用户定义的类。 我想设置ComboBox的SelectedValuePath属性,以便ComboBox项的值表示MyClass对象。

我尝试将SelectedValuePath属性设置为. this ,但是没有运气。 有人可以建议我实现这一目标的方法吗?

示例代码:

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