![](/img/trans.png)
[英]WPF ComboBox bound with object list and display less different value in selected item than in the item 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.SelectedItem
為MyClass
並且完全不設置SelectedValuePath
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.