2015-04-17 66 views
0

我已看過一些樣品如WPF組合框結合問題:使用對象

但它是週五,我的眼睛似乎廣場。我看不到我的錯誤:

public ObservableCollection<Customer> Customers { get; private set; } 

public MainWindow() 
{    
    InitializeComponent(); 
    this.Customers = new ObservableCollection<Customer>(); 
    this.Customers.Add(new Customer() { ID = 123, Name = "John Doe" }); 
    this.Customers.Add(new Customer() { ID = 456, Name = "Doe John" }); 
} 

和我的XAML:

<ComboBox x:Name="cbCustomer" 
      ItemsSource="{Binding Customer}" 
      DisplayMemberPath="Name" 
      SelectedValuePath="ID"> 
</ComboBox> 

我已閱讀,您可以使用一個DataTemplate或從上面的代碼。我喜歡簡單的下拉列表中,就像在Web表單:<asp:dropdownlist etc>只有在WPF

電流輸出:

very white

回答

2

您沒有設置任何地方DataContext

InitializeComponent(); 
this.Customers = new ObservableCollection<Customer>(); 
this.DataContext = this; 
this.Customers.Add(new Customer() { ID = 123, Name = "John Doe" }); 
this.Customers.Add(new Customer() { ID = 456, Name = "Doe John" }); 

ItemsSource的應是Customers,而不是Customer。與物業名稱相同,不是物品的類型

<ComboBox ... ItemsSource="{Binding Customers}"> 
+0

必須等待接受答案,但它是正確的 –