[英]WPF - Display Concatenated String in the ComboBox Items
我正在使用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";
我能夠顯示“客戶在組合內的名字”框。 但是,我想顯示客戶的名字和姓氏的組合 。 以下是我的客戶實體課程
[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; }
}
這樣做:
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綁定,而不是手動在后面的代碼中進行綁定,這只是一個想法! 希望能有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.