为什么我的 WPF CompboBox 是空的

Why is my WPF CompboBox Empty

我知道这是一个常见问题,但据我从其他帖子中看到的,我做的一切都是正确的......显然我不是。

后面代码中我的C#

        InitializeComponent();
        cboCustomerIds.DataContext = new Customers();

我在业务对象层的 C#

namespace BusinessObjects
{
public class Customers
{
    public class Customer
    {
        public Int64 CustomerId { get; set; }
    }

    public List<int> CustomerIds { get; set; }

    public Customers()
    {
        DataLayer dl = new DataLayer();
        SqlDataReader reader = dl.GetSqlDataReader("GetCustomerIds");
        List<Int64> CustomerIds = new List<Int64>();
        try
        {
            if (reader.HasRows)
            {
                do
                {
                    Int64 thing = 0;
                    thing = (Int64)reader["CustomerId"];
                    CustomerIds.Add(thing);
                    int count = CustomerIds.Count;
                    Int64 id = CustomerIds[count-1];
                }
                while (reader.Read());
            }
        }
        catch
        {
            throw;
        }
        finally
        {
            reader.Close();
        }
    }
}
}

C# 对 CustomerId 进行计数,因此我可以调试并确认项目正在从数据库返回并添加到列表中。 我的 XAML

    <Grid>
    <ComboBox x:Name="cboCustomerIds" ItemsSource="{Binding CustomerIds}" DisplayMemberPath="CustomerId" Width="120">
    </ComboBox>
</Grid>

构建无误并运行。它调用 Customers 构造函数并构建 CustomerIds 列表,但 cboCustomerIds 显示一个空列表。

仔细检查你的代码,这些代码永远不会被引用。

    public class Customer
    {
        public Int64 CustomerId { get; set; }
    }

    public List<int> CustomerIds { get; set; }

您从数据库中读取数据并将它们添加到

List<Int64> CustomerIds = new List<Int64>();

不喜欢

public List<int> CustomerIds { get; set; }

但是 ComboBox 与 public List<int> CustomerIds { get; set; }

绑定

所以一切看起来都很奇怪。

这是一个例子:

 public class Customers
{


    public List<int> CustomerIds { get; set; }

    public Customers()
    {

        CustomerIds = new List<int>();
        for (int i = 1; i < 10; i++)
        {
            CustomerIds.Add(i);
        }
    }


}

您应该首先删除 DisplayMemberPath。

我猜你的需求是:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        cboCustomerIds.DataContext = new Customers();
    }
}

/// <summary>
/// ViewModel
/// </summary>
public class Customers
{
    public List<Customer> LstCustomer { get; set; }

    public Customers()
    {
        LstCustomer = new List<Customer>();

        //get data from DB, here is an example
        for (int i = 1; i < 10; i++)
        {
            Customer c = new Customer();
            c.CustomerId = i;
            c.CustomerName = "name" + i;
            c.CustomerAge = 10 + i;

            LstCustomer.Add(c);
        }
    }


}

/// <summary>
/// Model
/// </summary>
public class Customer
{
    public int CustomerId { get; set; }
    public string CustomerName { get; set; }
    public int CustomerAge { get; set; }
}

View:

 <ComboBox x:Name="cboCustomerIds" ItemsSource="{Binding LstCustomer}" DisplayMemberPath="CustomerId" />