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";
精彩评论