简体   繁体   English

WPF将对象绑定到组合框

[英]WPF binding object to combobox

I need to bind objects stored in a list to a ComboBox. 我需要将存储在列表中的对象绑定到ComboBox。 Basically I need to update the list of ComboBox items dynamically for a continuous set of operations. 基本上,我需要动态地更新ComboBox项的列表,以进行连续的操作。 This is my code: 这是我的代码:

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();
            }
        }
}

and I call it in main like this: 我这样称呼它为:

private void FillComboBox()
    {
        cmbItems.ItemsSource = broker.FillComboBox();
    }

and it apparently does nothing. 它显然什么也没做。 Am I missing something? 我想念什么吗?

The code is right but my compiler was rly confused so that was the issue. 代码是正确的,但是我的编译器非常混乱,所以这就是问题所在。 Simple reopen solution helped! 简单的重新打开解决方案有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM