簡體   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