对要分配给Combobox的列表进行排序

unc*_*cia 1 .net c# wpf generic-list .net-4.5

我有一个字符串列表让我们说

List<string> mia = new list<string>;
Run Code Online (Sandbox Code Playgroud)

我正在从数据库中添加数据

using (SqlCommand command = new SqlCommand("SELECT xyz FROM table",sqlConnection1))
{
     sqlConnection1.Open();
     using (SqlDataReader reader = command.ExecuteReader())
     {
          while (reader.Read())
          {
               mia.Add(reader["xyz"].ToString());
          }

     }
     sqlConnection1.Close();                     
}
Run Code Online (Sandbox Code Playgroud)

数据已成功添加到其中.

combobox.ItemsSource =mia;
Run Code Online (Sandbox Code Playgroud)

即使这样也可以

但是当我尝试做的时候

comboOpthol.ItemsSource =mia.Sort();
Run Code Online (Sandbox Code Playgroud)

intellisense throws error无法隐式转换类型"void" System.collections.IEnumerable.为什么会出现这个错误.我的列表包含了为什么显示无效的所有数据?方式列表定义列表是否有问题?

Ere*_*mez 8

因为该Sort()方法没有返回值(它对列表进行排序并返回void).您需要先排序,然后将列表分配给项目源:

mia.Sort();
comboOpthol.ItemsSource = mia;
Run Code Online (Sandbox Code Playgroud)