如何使用数据绑定执行此操作

时间:2011-01-06 20:01:09

标签: c# data-binding

这是一个用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”填充组合框吗?

2 个答案:

答案 0 :(得分:3)

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

这是你想要的吗?

答案 1 :(得分:1)

另一种方法:

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