C# WPF-在组合框项目中显示连接的字符串

C# WPF-在组合框项目中显示连接的字符串,c#,wpf,combobox,C#,Wpf,Combobox,我正在使用C#创建WPF应用程序。我正在尝试从组合框内的列表中加载项目。我做了以下工作: customersList = context.Customers.Where(c => c.IsDeleted == false).ToList<.Customer>().OrderBy(x => x.CustomerId); cmbCustomer.ItemsSource = customersList; cmbCustomer.DisplayMemberPath = "F

我正在使用C#创建WPF应用程序。我正在尝试从组合框内的
列表中加载项目。我做了以下工作:

 customersList = context.Customers.Where(c => c.IsDeleted == false).ToList<.Customer>().OrderBy(x => x.CustomerId);
 cmbCustomer.ItemsSource = customersList;
 cmbCustomer.DisplayMemberPath = "FirstName";
 cmbCustomer.SelectedValuePath = "CustomerId";
这样做:

          var customersList = (from c in context.Customers
                                where c.IsDeleted == false
                                select new
                                {
                                    Name = c.FirstName + " " + c.LastName,
                                    c.CustomerId
                                }).ToList();

     cmbCustomer.ItemsSource = customersList;
     cmbCustomer.DisplayMemberPath = "Name";
     cmbCustomer.SelectedValuePath = "CustomerId";

我在这里写信。所以我的代码可能有错误。很抱歉。

如果您愿意编辑实体类(另一个更简洁的选项是使用单独的客户视图模型类),则可以添加以下属性:

public string FullName {
   get {
      return FirstName + " " + LastName;
   }
}
然后,对于显示成员路径:

cmbCustomer.DisplayMemberPath = "FullName";

您可能还想考虑使用XAML绑定,而不是手动在代码后面进行绑定,只是一个想法!希望有帮助。

它不起作用!!我遇到了一个异常:不支持直接绑定到存储查询(DbSet、DbQuery、DbSqlQuery、DbRawSqlQuery)的数据绑定。而是用数据填充数据库集,例如通过在数据库集上调用Load,然后绑定到本地数据。对于WPF,绑定到DbSet.Local。对于WinForms,绑定到DbSet.Local.ToBindingList()。对于ASP.NET WebForms,您可以绑定到对查询调用ToList()的结果,或使用模型绑定,有关详细信息,请参阅。找到它ToList()部分缺失!!:)正在将此答案更新为正确版本!!
cmbCustomer.DisplayMemberPath = "FullName";