wpf 如何将组合框项目放在列表中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38307460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:52:37  来源:igfitidea点击:

How to put combobox Items in a list?

c#sql-serverwpfcombobox

提问by Little Programmer

I added Items to a combobox using:

我使用以下方法将项目添加到组合框:

SqlDataReader sqlReader = sqlCmd.ExecuteReader();
while (sqlReader.Read())
{
    string name = sqlReader.GetString(0);
    combobox1.Items.Add(name);
}
sqlReader.Close();
conn.Close();

Now I want to put these value in a string list. Is that possible and how can I do that?

现在我想把这些值放在一个字符串列表中。这可能吗,我该怎么做?

回答by MANISH KUMAR CHOUDHARY

Simply you can do something like

简单地你可以做类似的事情

string[] items = new string[combobox1.Items.Count];  

 for(int i = 0; i < combobox1.Items.Count; i++)
   {
       items[i] = combobox1.Items[i].ToString();
   }

Or if want to Create a string list directly from reader object than

或者,如果想直接从阅读器对象创建一个字符串列表而不是

var itemList=new List<string>();
SqlDataReader sqlReader = sqlCmd.ExecuteReader();
                while (sqlReader.Read())
                {

                    string name = sqlReader.GetString(0);
                    combobox1.Items.Add(name);
                    itemList.Add(name);
                }
                sqlReader.Close();
                conn.Close();
            }

Use of LINQ will make you job very easier

使用 LINQ 会让你的工作变得更轻松

var arr = combobox1.Items.Cast<Object>()
      .Select(item => item.ToString()).ToArray();

回答by Brendon

private List<string> ComboBoxList = new List<string>();

Create this outside of the method you are currently in. This List will allow you to use it in any method within the class.

在您当前所在的方法之外创建它。此列表将允许您在类中的任何方法中使用它。

private List<string> ComboBoxList;

or Try this instead of the top piece of code. They both work.

或者试试这个而不是最上面的一段代码。他们都工作。

SqlDataReader sqlReader = sqlCmd.ExecuteReader();
                while (sqlReader.Read())
            {

                string name = sqlReader.GetString(0);
                combobox1.Items.Add(name);
                comboBoxList.Add(name);
            }
            sqlReader.Close();
            conn.Close();
        }

Create a new list and add each name to the list.

创建一个新列表并将每个名称添加到列表中。

回答by ángel Ibá?ez

I'm sure there's a more elegant method, but this works

我确定有更优雅的方法,但这有效

List<string> values = this.ComKeyType.Items
                      .Cast<object>()
                      .Select(x => x.ToString().Split('=')[1].Replace(" }", "").Trim())
                      .ToList();

回答by Rahul Hendawe

You probably want this:

你可能想要这个:

List<String>  cbList = new List<String>();

var lstItems= ComboBox1.Items.Cast<Object>()
          .Select(item => item.ToString()).ToList();

cbList.Add(lstItems); 

回答by mechanic

You would really need to separate concerns in your code to make it maintainable.

您确实需要在代码中分离关注点以使其可维护。

If you want to bind a list to combobox, you can do it in XAML:

如果要将列表绑定到组合框,可以在 XAML 中进行:

<ComboBox ItemsSource="{Binding MyList}" SelectedItem="{Binding CurrentItem}" />

You'd better make an ObservableCollection property in your view model:

你最好在你的视图模型中创建一个 ObservableCollection 属性:

public ObservableCollection<string> MyList {get; private set;}

and initialize it in your view model's constructor:

并在您的视图模型的构造函数中初始化它:

MyList = new ObservableCollection<string>(GetNames());

GetNames()here is the method where your SQL code is incapsulated:

GetNames()这是封装 SQL 代码的方法:

private List<string> GetNames() {
    var myList = new List<string>();
    SqlDataReader sqlReader = sqlCmd.ExecuteReader();
    while (sqlReader.Read())
        {
            string name = sqlReader.GetString(0);
            myList.Add(name);
        }
    sqlReader.Close();
    conn.Close();

    return myList;
}