开发者

How to do this with Databinding

Here's a function to populate a combobox with SaveState.SaveName values. As you can see I'm not using ItemsSource I'm looking for a better way to do this function.

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

I tried this:

this.comboBox1.ItemsSource = names; 
开发者_C百科

But the combobox was populated with my datatype. Can I use the ItemsSource in such a way that it populates the combobox with the data member "SaveName"?


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

Is this what you want?


Another way to do it:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜