C# WPF将对象绑定到combobox
我需要将列表中存储的对象绑定到组合框。基本上,我需要为一组连续的操作动态更新组合框项目列表。这是我的代码:C# WPF将对象绑定到combobox,c#,wpf,combobox,C#,Wpf,Combobox,我需要将列表中存储的对象绑定到组合框。基本上,我需要为一组连续的操作动态更新组合框项目列表。这是我的代码: class Broker { public List<Item> FillComboBox() { List<Item> itemList = new List<Item>(); try { string sql = "SELECT * FROM Sklad"
class Broker
{
public List<Item> FillComboBox()
{
List<Item> itemList = new List<Item>();
try
{
string sql = "SELECT * FROM Sklad";
cmd = new SqlCommand(sql, connection);
connection.Open();
System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Item item = new Item();
item.Id = Convert.ToInt32(reader["Id"].ToString());
item.Znacka = reader["Znacka"].ToString();
item.Model = reader["Model"].ToString();
item.Typ = reader["Typ"].ToString();
item.Farba = reader["Farba"].ToString();
item.Mnozstvo = Convert.ToInt32(reader["Mnozstvo"].ToString());
item.NakupnaCena = Convert.ToDouble(reader["NakupnaCena"].ToString());
item.PredajnaCena = Convert.ToDouble(reader["PredajnaCena"].ToString());
itemList.Add(item);
}
return itemList;
}
catch (Exception eX)
{
MessageBox.Show(eX.Message);
return null;
}
finally
{
if (connection != null)
{
connection.Close();
}
}
}
它显然什么也没做。我遗漏了什么吗?代码是正确的,但我的编译器非常混乱,这就是问题所在。简单的重新打开解决方案帮助 检查以确保FillCombobox实际返回的列表中包含任何内容。作为测试,我将添加item.ToString,以确保您的comboBox使用默认类型。因此,请尝试itemList.Additem.ToString;可能是您的combobox根本不知道如何呈现您的项目类型。为您显示XAML combobox哦,我的编译器被我所做的操作弄糊涂了。。。简单的重新打开解决方案有帮助,但谢谢!
private void FillComboBox()
{
cmbItems.ItemsSource = broker.FillComboBox();
}