Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用数据绑定实现这一点_C#_Data Binding - Fatal编程技术网

C# 如何使用数据绑定实现这一点

C# 如何使用数据绑定实现这一点,c#,data-binding,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; thi

下面是一个用
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); 
    }
}
但是组合框中填充了我的数据类型。我是否可以使用ItemsSource来填充组合框中的数据成员“SaveName”

这是你想要的吗

这就是你想要的吗?

另一种方法:

this.comboBox1.DataSource = names;
this.comboBox1.DisplayMember = "SaveName";
另一种方法是:

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