2011-01-06 36 views
0

以下是一個用SaveState.SaveName值填充組合框的功能。正如你可以看到我沒有使用ItemsSource我正在尋找更好的方法來做這個功能。如何使用數據綁定來執行此操作

public void RestoreState(List<SaveState> names) 
{ 
    foreach (SaveState st in names) 
    { 
     Label l = new Label(); 
     l.Content = st.SaveName; 
     this.comboBox1.Items.Add(l); 
    } 
} 

我嘗試這樣做:

this.comboBox1.ItemsSource = names; 

但組合框與我的數據類型填充。我可以使用ItemsSource來填充數據成員「SaveName」的組合框嗎?

回答

3
this.comboBox1.ItemSource = names.Select(o=>o.SaveName) 

這是你想要的嗎?

1

另一種方式來做到這一點:

this.comboBox1.DataSource = names; 
this.comboBox1.DisplayMember = "SaveName";