2013-02-22 93 views
0

我有一個問題。當Binding項目到ListBox它垂直添加它們。例如,其中ItemsSource = "{Binding Path = Name}"NameBill,我得到了ListBox中每個字符在線的輸出(如下所示:B \ ni \ nl \ nl)。我在哪裏做錯了?綁定項目到列表框垂直添加它們

XAML:

<Window x:Class="ObservableColl.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 

    <Grid> 
     <ListBox HorizontalAlignment="Left" Height="244" Margin="313,28,0,0" VerticalAlignment="Top" Width="177" ItemsSource="{Binding Path=Name}"/> 
    </Grid> 
</Window> 

C#:

class Customer 
    { 
     public string Name{get; set;} 
    } 

    class Customers 
    { 
     public ObservableCollection<Customer> customerOC{get; set;} 
     public Customers() 
     { 
      customerOC = new ObservableCollection<Customer>(); 
     } 
     public void AddCustomer(Customer c) 
     { 
      customerOC.Add(c); 
     } 
    } 

public partial class MainWindow:Window 
{ 
    Customers customers{get; set;} 
    public MainWindow() 
    { 
     InitializeComponent(); 
     customers = new Customers(); 

     customers.AddCustomer(new Customer(){Name = "Frank"}); 

     this.DataContext = customers; 
    } 
} 
+0

在類Customers的構造函數中,'customer'是什麼?我看不到這個變量的定義。 – Abbas 2013-02-22 17:51:33

+0

對不起,它是爲了顧客。編輯* – jonjohnson 2013-02-22 17:59:14

+0

您可以顯示完整的XAML – devdigital 2013-02-22 18:03:10

回答

1

DataContext設置爲客戶。 ItemsSource應設置爲ObservableCollectionDisplayMemberPath用於顯示客戶的正確財產。

<ListBox ItemsSource="{Binding customerOC}" DisplayMemberPath="Name" /> 
+0

謝謝完美的作品 – jonjohnson 2013-02-22 18:12:19

相關問題