我正在使用C#创建WPF应用程序。我正在尝试从组合框中的List<Customer>
加载项目。我做了以下事情:
customersList = context.Customers.Where(c => c.IsDeleted == false).ToList<.Customer>().OrderBy(x => x.CustomerId);
cmbCustomer.ItemsSource = customersList;
cmbCustomer.DisplayMemberPath = "FirstName";
cmbCustomer.SelectedValuePath = "CustomerId";
我能够在组合框中显示CustomerName的FirstName。 但是,我想显示名字和客户姓氏的组合。以下是我的客户实体类
[Table("Customer")]
public class Customer
{
[Key]
public int CustomerId { get; set; }
[Column("FirstName", TypeName = "ntext")]
[MaxLength(100)]
public string FirstName { get; set; }
[Column("LastName", TypeName = "ntext")]
[MaxLength(100)]
public string LastName { get; set; }
[Column("Email", TypeName = "ntext")]
[MaxLength(100)]
public string Email { get; set; }
[Column("Company", TypeName = "ntext")]
[MaxLength(100)]
public string Company { get; set; }
[Column("Phone", TypeName = "ntext")]
[MaxLength(100)]
public string Phone { get; set; }
[Column("Address", TypeName = "ntext")]
[MaxLength(500)]
public string Address { get; set; }
[Column("IsDeleted", TypeName = "bit")]
public bool IsDeleted { get; set; }
}
答案 0 :(得分:1)
这样做:
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";
我在这里写作。所以我的代码可能有错误。对不起。
答案 1 :(得分:0)
如果您愿意编辑实体类(另一个清洁选项是拥有单独的客户视图模型类),您可以添加以下属性:
public string FullName {
get {
return FirstName + " " + LastName;
}
}
然后为您的显示成员路径:
cmbCustomer.DisplayMemberPath = "FullName";
您可能还想考虑使用XAML绑定而不是手动在代码后面进行绑定,只是想一想!希望有所帮助。